Skip to content

Commit

Permalink
feat(resolver): add support for parameterMacro option
Browse files Browse the repository at this point in the history
This change is specific to OpenAPI 3.1.0 strategy.

Refs #2748
  • Loading branch information
char0n committed Jan 17, 2023
1 parent fb8a99d commit 33cbf40
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy
props: {
useCircularStructures: true,
allowMetaPatches: false,
parameterMacro: null,
},
init({
useCircularStructures = this.useCircularStructures,
allowMetaPatches = this.allowMetaPatches,
parameterMacro = this.parameterMacro,
} = {}) {
this.name = 'openapi-3-1-swagger-client';
this.useCircularStructures = useCircularStructures;
this.allowMetaPatches = allowMetaPatches;
this.parameterMacro = parameterMacro;
},
methods: {
async dereference(file, options) {
Expand All @@ -41,6 +44,7 @@ const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy
options,
useCircularStructures: this.useCircularStructures,
allowMetaPatches: this.allowMetaPatches,
parameterMacro: this.parameterMacro,
});
const dereferencedElement = await visitAsync(refSet.rootRef.value, visitor, {
keyMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isPrimitiveElement,
isStringElement,
visit,
toValue,
includesClasses,
} from '@swagger-api/apidom-core';
import {
Expand Down Expand Up @@ -40,17 +41,28 @@ import {

const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];

const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.compose({
props: {
useCircularStructures: true,
allowMetaPatches: false,
},
init({ useCircularStructures, allowMetaPatches }) {
const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.init(
function _OpenApi3_1SwaggerClientDereferenceVisitor({
useCircularStructures = true,
allowMetaPatches = false,
parameterMacro = null,
}) {
const instance = this;
let parameterMacroOperation = null;

// props
this.useCircularStructures = useCircularStructures;
this.allowMetaPatches = allowMetaPatches;
},
methods: {
async ReferenceElement(referenceElement, key, parent, path, ancestors) {
this.parameterMacro = parameterMacro;

// methods
this.ReferenceElement = async function _ReferenceElement(
referenceElement,
key,
parent,
path,
ancestors
) {
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(ancestors);

// skip already identified cycled Path Item Objects
Expand Down Expand Up @@ -119,6 +131,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
ancestors: ancestorsLineage,
allowMetaPatches: this.allowMetaPatches,
useCircularStructures: this.useCircularStructures,
parameterMacro: this.parameterMacro,
});
fragment = await visitAsync(fragment, visitor, { keyMap, nodeTypeGetter: getNodeType });

Expand Down Expand Up @@ -176,9 +189,15 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c

// transclude the element for a fragment
return fragment;
},

async PathItemElement(pathItemElement, key, parent, path, ancestors) {
};

this.PathItemElement = async function _PathItemElement(
pathItemElement,
key,
parent,
path,
ancestors
) {
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(ancestors);

// ignore PathItemElement without $ref field
Expand Down Expand Up @@ -242,6 +261,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
ancestors: ancestorsLineage,
allowMetaPatches: this.allowMetaPatches,
useCircularStructures: this.useCircularStructures,
parameterMacro: this.parameterMacro,
});
referencedElement = await visitAsync(referencedElement, visitor, {
keyMap,
Expand Down Expand Up @@ -302,9 +322,15 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c

// transclude referencing element with merged referenced element
return mergedPathItemElement;
},

async SchemaElement(referencingElement, key, parent, path, ancestors) {
};

this.SchemaElement = async function _SchemaElement(
referencingElement,
key,
parent,
path,
ancestors
) {
const [ancestorsLineage, directAncestors] = this.toAncestorLineage(ancestors);

// skip current referencing schema as $ref keyword was not defined
Expand Down Expand Up @@ -414,6 +440,7 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c
options: this.options,
useCircularStructures: this.useCircularStructures,
allowMetaPatches: this.allowMetaPatches,
parameterMacro: this.parameterMacro,
ancestors: ancestorsLineage,
});
referencedElement = await visitAsync(referencedElement, mergeVisitor, {
Expand Down Expand Up @@ -490,9 +517,31 @@ const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.c

// transclude referencing element with merged referenced element
return mergedSchemaElement;
},
},
});
};

this.OperationElement = {
enter(operationElement) {
parameterMacroOperation = operationElement;
},
leave() {
parameterMacroOperation = null;
},
};

this.ParameterElement = {
leave(parameterElement) {
if (typeof instance.parameterMacro !== 'function') return;

const pojoOperation =
parameterMacroOperation === null ? null : toValue(parameterMacroOperation);
const pojoParameter = toValue(parameterElement);
const defaultValue = instance.parameterMacro(pojoOperation, pojoParameter);

parameterElement.set('default', defaultValue);
},
};
}
);

export default OpenApi3_1SwaggerClientDereferenceVisitor;
/* eslint-enable camelcase */
7 changes: 6 additions & 1 deletion src/resolver/strategies/openapi-3-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const resolveOpenAPI31Strategy = async (options) => {
allowMetaPatches = false,
useCircularStructures = false,
skipNormalization = false,
parameterMacro = null,
} = options;
// determining BaseURI
const defaultBaseURI = 'https://smartbear.com/';
Expand Down Expand Up @@ -90,7 +91,11 @@ const resolveOpenAPI31Strategy = async (options) => {
},
dereference: {
strategies: [
OpenApi3_1SwaggerClientDereferenceStrategy({ allowMetaPatches, useCircularStructures }),
OpenApi3_1SwaggerClientDereferenceStrategy({
allowMetaPatches,
useCircularStructures,
parameterMacro,
}),
],
refSet,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT"
}
},
"components": {
"parameters": {
"limit": {
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"maximum": 100,
"format": "int32"
}
}
}
}
}
137 changes: 137 additions & 0 deletions test/resolver/strategies/openapi-3-1/__fixtures__/parameter-macro.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/v1"
}
],
"paths": {
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": [
"pets"
],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"maximum": 100,
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pets"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"post": {
"summary": "Create a pet",
"operationId": "createPets",
"tags": [
"pets"
],
"responses": {
"201": {
"description": "Null response"
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Pets": {
"type": "array",
"maxItems": 100,
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}
Loading

0 comments on commit 33cbf40

Please sign in to comment.