Skip to content

Commit

Permalink
fix(reference): fix bug in OpenAPI 3.1.0 Schema Object resolving (#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Jan 6, 2023
1 parent 3146621 commit 5819ac9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const OpenApi3_1ResolveVisitor = stampit({
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
const file = File({ uri: $refBaseURIStrippedHash });
const isUnknownURI = none((r: IResolver) => r.canRead(file), this.options.resolve.resolvers);
const isURL = !isUnknownURI;
const isExternal = !isUnknownURI && this.reference.uri !== $refBaseURIStrippedHash;

// ignore resolving external Reference Objects
Expand All @@ -222,9 +223,23 @@ const OpenApi3_1ResolveVisitor = stampit({
}

if (!has($refBaseURIStrippedHash, this.crawlingMap)) {
this.crawlingMap[$refBaseURIStrippedHash] = isUnknownURI
? this.reference
: this.toReference($refBaseURIStrippedHash);
try {
if (isUnknownURI || isURL) {
this.crawlingMap[$refBaseURIStrippedHash] = this.reference;
} else {
this.crawlingMap[$refBaseURIStrippedHash] = this.toReference(
url.unsanitize($refBaseURI),
);
}
} catch (error) {
if (isURL && error instanceof EvaluationJsonSchemaUriError) {
this.crawlingMap[$refBaseURIStrippedHash] = this.toReference(
url.unsanitize($refBaseURI),
);
} else {
throw error;
}
}
}
this.crawledElements.push(schemaElement);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$id": "https://swagger.io/schemas/user-profile",
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
}
},
"UserProfile": {
"$id": "https://swagger.io/schemas/user-profile",
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "https://swagger.io/schemas/user-profile"
}
}
},
"UserProfile": {
"$id": "https://swagger.io/schemas/user-profile",
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,19 @@ describe('resolve', function () {
});
});

context('given Schema Objects with $ref keyword containing URL', function () {
const fixturePath = path.join(rootFixturePath, '$ref-url');

specify('should resolve', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const refSet = await resolve(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});

assert.strictEqual(refSet.size, 1);
});
});

context(
'given Schema Objects with $ref keyword containing Uniform Resource Name',
function () {
Expand Down

0 comments on commit 5819ac9

Please sign in to comment.