Skip to content

Commit 4761414

Browse files
committed
update3
1 parent 0420e1f commit 4761414

5 files changed

Lines changed: 62 additions & 9 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Runs a security scan of an OpenAPI specification using OWASP ZAP.
263263

264264
* Executes inside Docker
265265
* Accepts an OpenAPI file or directory (default: `openapi`)
266-
* Relative `-f` paths are resolved from the git repository root (fallback: current working directory)
266+
* Relative `-f` paths are resolved from the git repository root first, then current working directory
267267
* For file paths, supports `.yml`/`.yaml` extension fallback
268268
* Skips execution if no OpenAPI specification can be resolved
269269

@@ -297,7 +297,7 @@ Validates an OpenAPI specification for schema correctness.
297297

298298
* Runs the OpenAPI validator in Docker
299299
* Accepts an OpenAPI file or directory (default: `openapi`)
300-
* Relative `-f` paths are resolved from the git repository root (fallback: current working directory)
300+
* Relative `-f` paths are resolved from the git repository root first, then current working directory
301301
* For file paths, supports `.yml`/`.yaml` extension fallback
302302
* Skips execution if no OpenAPI specification can be resolved
303303

@@ -591,7 +591,7 @@ Serves OpenAPI documentation locally using Docker.
591591
#### Behavior
592592

593593
* Accepts an OpenAPI file or directory (default: `openapi`)
594-
* Relative `-f` paths are resolved from the git repository root (fallback: current working directory)
594+
* Relative `-f` paths are resolved from the git repository root first, then current working directory
595595
* If a file is provided, mounts its parent directory
596596
* For file paths, supports `.yml`/`.yaml` extension fallback
597597
* Runs Nginx in the foreground

scripts/check-openapi-security.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,18 @@ if [[ "${OPENAPI_PATH}" = /* ]]; then
7474
# Absolute paths are used as-is.
7575
OPENAPI_ABS_PATH="${OPENAPI_PATH}"
7676
else
77-
# Relative paths are resolved from repository root.
77+
# Relative paths prefer repository root, then current working directory.
7878
REPO_ROOT="$(resolve_repo_root)"
79-
OPENAPI_ABS_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
79+
REPO_OPENAPI_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
80+
CWD_OPENAPI_PATH="$(pwd)/${OPENAPI_PATH}"
81+
82+
if [ -e "${REPO_OPENAPI_PATH}" ]; then
83+
OPENAPI_ABS_PATH="${REPO_OPENAPI_PATH}"
84+
elif [ -e "${CWD_OPENAPI_PATH}" ]; then
85+
OPENAPI_ABS_PATH="${CWD_OPENAPI_PATH}"
86+
else
87+
OPENAPI_ABS_PATH="${REPO_OPENAPI_PATH}"
88+
fi
8089
fi
8190

8291
# Allow extension fallback between .yml and .yaml.

scripts/check-openapi-validation.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,18 @@ if [[ "${OPENAPI_PATH}" = /* ]]; then
7676
# Absolute paths are used as-is.
7777
OPENAPI_ABS_PATH="${OPENAPI_PATH}"
7878
else
79-
# Relative paths are resolved from repository root.
79+
# Relative paths prefer repository root, then current working directory.
8080
REPO_ROOT="$(resolve_repo_root)"
81-
OPENAPI_ABS_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
81+
REPO_OPENAPI_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
82+
CWD_OPENAPI_PATH="$(pwd)/${OPENAPI_PATH}"
83+
84+
if [ -e "${REPO_OPENAPI_PATH}" ]; then
85+
OPENAPI_ABS_PATH="${REPO_OPENAPI_PATH}"
86+
elif [ -e "${CWD_OPENAPI_PATH}" ]; then
87+
OPENAPI_ABS_PATH="${CWD_OPENAPI_PATH}"
88+
else
89+
OPENAPI_ABS_PATH="${REPO_OPENAPI_PATH}"
90+
fi
8291
fi
8392

8493
# Allow extension fallback between .yml and .yaml.

scripts/run-openapi-docker.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,18 @@ if [[ "${OPENAPI_PATH}" = /* ]]; then
9595
# Absolute paths are used as-is.
9696
OPENAPI_ABS_PATH="${OPENAPI_PATH}"
9797
else
98-
# Relative paths are resolved from repository root.
98+
# Relative paths prefer repository root, then current working directory.
9999
REPO_ROOT="$(resolve_repo_root)"
100-
OPENAPI_ABS_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
100+
REPO_OPENAPI_PATH="${REPO_ROOT}/${OPENAPI_PATH}"
101+
CWD_OPENAPI_PATH="$(pwd)/${OPENAPI_PATH}"
102+
103+
if [ -e "${REPO_OPENAPI_PATH}" ]; then
104+
OPENAPI_ABS_PATH="${REPO_OPENAPI_PATH}"
105+
elif [ -e "${CWD_OPENAPI_PATH}" ]; then
106+
OPENAPI_ABS_PATH="${CWD_OPENAPI_PATH}"
107+
else
108+
OPENAPI_ABS_PATH="${REPO_OPENAPI_PATH}"
109+
fi
101110
fi
102111

103112
# Allow extension fallback between .yml and .yaml.

tests/bats/openapi-scripts.bats

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,29 @@ YAML
189189
docker_call="$(cat "$DOCKER_LOG")"
190190
[[ "$docker_call" == *"/mail-examples/mail-example-openapi/openapi/openapi.yaml:/openapi.yaml"* ]]
191191
}
192+
193+
@test "check-openapi-validation default path works for copied script in nested project scripts folder" {
194+
repo="$(make_temp_repo)"
195+
install_docker_stub "$repo"
196+
197+
mkdir -p "$repo/mail-examples/mail-example-openapi/scripts"
198+
mkdir -p "$repo/mail-examples/mail-example-openapi/openapi"
199+
cp "$PROJECT_ROOT/scripts/check-openapi-validation.sh" \
200+
"$repo/mail-examples/mail-example-openapi/scripts/check-openapi-validation.sh"
201+
chmod +x "$repo/mail-examples/mail-example-openapi/scripts/check-openapi-validation.sh"
202+
203+
cat >"$repo/mail-examples/mail-example-openapi/openapi/openapi.yaml" <<'YAML'
204+
openapi: 3.0.0
205+
info:
206+
title: Nested Local API
207+
version: 1.0.0
208+
paths: {}
209+
YAML
210+
211+
export DOCKER_LOG="$repo/docker.log"
212+
run bash -c "cd '$repo/mail-examples/mail-example-openapi' && bash ./scripts/check-openapi-validation.sh"
213+
214+
[ "$status" -eq 0 ]
215+
docker_call="$(cat "$DOCKER_LOG")"
216+
[[ "$docker_call" == *"/mail-example-openapi/openapi/openapi.yaml:/openapi.yaml"* ]]
217+
}

0 commit comments

Comments
 (0)