Skip to content

Commit

Permalink
Merge pull request #122 from seriousme/fix-ref-validation-for-recursi…
Browse files Browse the repository at this point in the history
…ve-refs

fix: recursive refs work during validation as well
  • Loading branch information
seriousme authored Nov 13, 2023
2 parents a31d4b6 + 035040c commit 3d43e16
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]
### Changed

## [v2.1.4] 13-11-2023
### Changed
- fix: recursive refs work during validation as well
- fix: update index.d.ts to include single string errors on validate

## [v2.1.3] 12-11-2023
Expand Down
13 changes: 6 additions & 7 deletions resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function resolve(tree, replace) {
return undefined;
}

if (replace === false) {
treeObj = structuredClone(tree);
}

const pointers = {};
for (const word of pointerWords) {
pointers[word] = [];
Expand Down Expand Up @@ -147,20 +151,15 @@ function resolve(tree, replace) {
const { ref, id, path } = item;
const decodedRef = decodeURIComponent(ref);
const fullRef = decodedRef[0] !== "#" ? decodedRef : `${id}${decodedRef}`;
const uri = resolveUri(fullRef, anchors);
if (replace) {
applyRef(path, uri);
}
applyRef(path, resolveUri(fullRef, anchors));
}

for (const item of pointers.$dynamicRef) {
const { ref, path } = item;
if (!dynamicAnchors[ref]) {
throw new Error(`Can't resolve $dynamicAnchor : '${ref}'`);
}
if (replace) {
applyRef(path, dynamicAnchors[ref]);
}
applyRef(path, dynamicAnchors[ref]);
}

return treeObj;
Expand Down
7 changes: 7 additions & 0 deletions test/test-validation-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function localFile(fileName) {
}

const invalidRefsSpec = localFile("./validation/invalid-refs.yaml");
const recursiveRefsSpec = localFile("./validation/openapi-recursive.v3.json");

test("invalid refs in YAML fail validation", async (t) => {
const validator = new Validator();
Expand All @@ -19,3 +20,9 @@ test("invalid refs in YAML fail validation", async (t) => {
"correct error message",
);
});

test("recursive refs pass validation", async (t) => {
const validator = new Validator();
const res = await validator.validate(recursiveRefsSpec);
assert.equal(res.valid, true, "validation succeeds");
});
6 changes: 6 additions & 0 deletions test/test-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const inlinedRefs = "x-inlined-refs";
async function testVersion(version) {
test(`version ${version} works`, async (t) => {
const petStoreSpec = importJSON(`./v${version}/petstore.json`);
const copyPetStoreSpec = structuredClone(petStoreSpec);
const validator = new Validator();

const res = await validator.validate(petStoreSpec);
Expand All @@ -36,6 +37,11 @@ async function testVersion(version) {
version,
"petstore spec version matches expected version",
);
assert.deepEqual(
petStoreSpec,
copyPetStoreSpec,
"original petstore spec is not modified",
);
});
}

Expand Down
76 changes: 76 additions & 0 deletions test/validation/openapi-recursive.v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"openapi": "3.0.0",
"servers": [
{
"url": "http://localhost/v2"
}
],
"info": {
"title": "Recursive Test specification",
"description": "testing the fastify openapi glue using recursive schema",
"version": "0.1.0"
},
"paths": {
"/recursive": {
"post": {
"operationId": "postRecursive",
"summary": "Test recursive parameters",
"responses": {
"200": {
"description": "ok",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/bodyObject"
}
}
}
}
},
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/bodyObject"
}
}
},
"required": true
}
}
}
},

"components": {
"schemas": {
"bodyObject": {
"type": "object",
"additionalProperties": false,
"properties": {
"str1": {
"type": "string"
},
"arr": {
"type": "array",
"items": {
"type": "string"
}
},
"objRef": {
"$ref": "#/components/schemas/bodyObject"
},
"arrRef": {
"type": "array",
"items": {
"$ref": "#/components/schemas/bodyObject"
}
},
"refStr": {
"$ref": "#/components/schemas/bodyObject/properties/arrRef/items/properties/arr/items"
}
},
"required": ["str1"]
}
}
}
}

0 comments on commit 3d43e16

Please sign in to comment.