Skip to content

Commit

Permalink
test(resolver): add tests for OpenAPI 3.1.0 allowMetaPatches option
Browse files Browse the repository at this point in the history
Closes #2760
  • Loading branch information
char0n committed Jan 16, 2023
1 parent 492c33c commit 8a8b08b
Show file tree
Hide file tree
Showing 34 changed files with 1,425 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,8 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
const objectFragment = fragment;
// apply meta patch only when not already applied
if (typeof objectFragment.get('$$ref') === 'undefined') {
const absoluteJSONPointerURL = url.resolve(
reference.uri,
referenceElement.$ref?.toValue()
);
objectFragment.set('$$ref', absoluteJSONPointerURL);
const absoluteURI = url.resolve(reference.uri, referenceElement.$ref?.toValue());
objectFragment.set('$$ref', absoluteURI);
}
}

Expand Down Expand Up @@ -226,11 +223,8 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
if (this.allowMetaPatches) {
// apply meta patch only when not already applied
if (typeof mergedResult.get('$$ref') === 'undefined') {
const absoluteJSONPointerURL = url.resolve(
reference.uri,
pathItemElement.$ref?.toValue()
);
mergedResult.set('$$ref', absoluteJSONPointerURL);
const absoluteURI = url.resolve(reference.uri, pathItemElement.$ref?.toValue());
mergedResult.set('$$ref', absoluteURI);
}
}

Expand All @@ -255,13 +249,14 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
}

// compute baseURI using rules around $id and $ref keywords
const retrieveURI = this.reference.uri;
const $refBaseURI = resolveSchema$refField(retrieveURI, referencingElement);
let { reference } = this;
let { uri: retrievalURI } = reference;
const $refBaseURI = resolveSchema$refField(retrievalURI, referencingElement);
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
const file = File({ uri: $refBaseURIStrippedHash });
const isUnknownURI = !this.options.resolve.resolvers.some((r) => r.canRead(file));
const isURL = !isUnknownURI;
const isExternal = isURL && this.reference.uri !== $refBaseURIStrippedHash;
const isExternal = isURL && retrievalURI !== $refBaseURIStrippedHash;

// ignore resolving external Schema Objects
if (!this.options.resolve.external && isExternal) {
Expand All @@ -272,13 +267,11 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
this.indirections.push(referencingElement);

// determining reference, proper evaluation and selection mechanism
let reference;
let referencedElement;

try {
if (isUnknownURI || isURL) {
// we're dealing with canonical URI or URL with possible fragment
reference = this.reference;
const selector = $refBaseURI;
referencedElement = uriEvaluate(
selector,
Expand All @@ -288,6 +281,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
} else {
// we're assuming here that we're dealing with JSON Pointer here
reference = await this.toReference(url.unsanitize($refBaseURI));
retrievalURI = reference.uri;
const selector = uriToPointer($refBaseURI);
referencedElement = maybeRefractToSchemaElement(
// @ts-ignore
Expand All @@ -303,6 +297,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
if (isAnchor(uriToAnchor($refBaseURI))) {
// we're dealing with JSON Schema $anchor here
reference = await this.toReference(url.unsanitize($refBaseURI));
retrievalURI = reference.uri;
const selector = uriToAnchor($refBaseURI);
referencedElement = $anchorEvaluate(
selector,
Expand All @@ -312,6 +307,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
} else {
// we're assuming here that we're dealing with JSON Pointer here
reference = await this.toReference(url.unsanitize($refBaseURI));
retrievalURI = reference.uri;
const selector = uriToPointer($refBaseURI);
referencedElement = maybeRefractToSchemaElement(
// @ts-ignore
Expand Down Expand Up @@ -363,7 +359,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
$ref: referencingElement.$ref?.toValue(),
});
// annotate referenced element with info about origin
jsonSchemaBooleanElement.setMetaProperty('ref-origin', reference.uri);
jsonSchemaBooleanElement.setMetaProperty('ref-origin', retrievalURI);

return jsonSchemaBooleanElement;
}
Expand All @@ -373,10 +369,10 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
ancestorsLineage.includes(memberElement)
);
if (hasCycle && !this.useCircularStructures) {
if (url.isHttpUrl(reference.uri) || url.isFileSystemPath(reference.uri)) {
if (url.isHttpUrl(retrievalURI) || url.isFileSystemPath(retrievalURI)) {
// make the referencing URL or file system path absolute
const absoluteURI = url.resolve(reference.uri, referencingElement.$ref?.toValue());
referencingElement.set('$ref', absoluteURI);
const baseURI = url.resolve(retrievalURI, $refBaseURI);
referencingElement.set('$ref', baseURI);
}

// skip processing this schema but traverse all it's child schemas
Expand All @@ -402,14 +398,14 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
$ref: referencingElement.$ref?.toValue(),
});
// annotate fragment with info about origin
mergedSchemaElement.setMetaProperty('ref-origin', reference.uri);
mergedSchemaElement.setMetaProperty('ref-origin', retrievalURI);

// allowMetaPatches option processing
if (this.allowMetaPatches) {
// apply meta patch only when not already applied
if (typeof mergedSchemaElement.get('$$ref') === 'undefined') {
const absoluteURI = url.resolve(reference.uri, referencingElement.$ref?.toValue());
mergedSchemaElement.set('$$ref', absoluteURI);
const baseURI = url.resolve(retrievalURI, $refBaseURI);
mergedSchemaElement.set('$$ref', baseURI);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$$ref": "/home/smartbear/ex.json#user-profile",
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$defs": {
"UserProfile": {
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "./ex.json#user-profile"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$$ref": "/home/smartbear/root.json#user-profile",
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
},
"UserProfile": {
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "#user-profile"
}
}
},
"UserProfile": {
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"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": "/home/smartbear/nested/ex.json",
"$id": "./nested/",
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$id": "./nested/",
"$ref": "./ex.json"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"$id": "./nested/",
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$$ref": "/home/smartbear/nested/ex.json",
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "object",
"properties": {
"avatar": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"$id": "./nested/",
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "./ex.json"
}
}
}
}
}
}
Loading

0 comments on commit 8a8b08b

Please sign in to comment.