Skip to content

Commit

Permalink
fix(reference): fix how OpenAPI 3.1.0 Reference Object is merged (#3215)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Oct 3, 2023
1 parent 87d49ef commit 77a6823
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
8 changes: 4 additions & 4 deletions packages/apidom-ls/test/fixtures/deref/dereferenced.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"userId": {
"name": "userId",
"in": "query",
"description": "override",
"required": true
"required": true,
"description": "override"
},
"indirection1": {
"name": "userId",
Expand All @@ -41,8 +41,8 @@
"externalRef": {
"name": "externalParameter",
"in": "query",
"description": "another ref",
"required": true
"required": true,
"description": "another ref"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import stampit from 'stampit';
import { hasIn, pathSatisfies, propEq, none } from 'ramda';
import { isUndefined, isNotUndefined } from 'ramda-adjunct';
import { propEq, none } from 'ramda';
import { isUndefined } from 'ramda-adjunct';
import {
isElement,
isPrimitiveElement,
isStringElement,
isMemberElement,
isObjectElement,
visit,
Element,
find,
cloneShallow,
cloneDeep,
toValue,
Element,
} from '@swagger-api/apidom-core';
import { evaluate as jsonPointerEvaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
import {
Expand Down Expand Up @@ -193,7 +194,7 @@ const OpenApi3_1DereferenceVisitor = stampit({
this.indirections.pop();

const mergeAndAnnotateReferencedElement = <T extends Element>(refedElement: T): T => {
const copy = cloneDeep(refedElement);
const copy = cloneShallow(refedElement);

// annotate fragment with info about original Reference element
copy.setMetaProperty('ref-fields', {
Expand All @@ -207,16 +208,19 @@ const OpenApi3_1DereferenceVisitor = stampit({
copy.setMetaProperty('ref-origin', reference.uri);

// override description and summary (outer has higher priority then inner)
const hasDescription = pathSatisfies(isNotUndefined, ['description'], referencingElement);
const hasSummary = pathSatisfies(isNotUndefined, ['summary'], referencingElement);

if (hasDescription && hasIn('description', refedElement)) {
// @ts-ignore
copy.description = referencingElement.description;
}
if (hasSummary && hasIn('summary', refedElement)) {
// @ts-ignore
copy.summary = referencingElement.summary;
if (isObjectElement(refedElement)) {
if (referencingElement.hasKey('description') && 'description' in refedElement) {
// @ts-ignore
copy.remove('description');
// @ts-ignore
copy.set('description', referencingElement.get('description'));
}
if (referencingElement.hasKey('summary') && 'summary' in refedElement) {
// @ts-ignore
copy.remove('summary');
// @ts-ignore
copy.set('summary', referencingElement.get('summary'));
}
}

return copy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"parent": {
"$ref": "#/components/schemas/User"
"parameters": {
"param1": {
"examples": {
"example1": {
"$ref": "#/components/parameters/param1",
"description": "test"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ describe('dereference', function () {
const dereferenced = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const parent = evaluate('/0/components/schemas/User/properties/parent', dereferenced);
const parent = evaluate('/0/components/parameters/param1/examples', dereferenced);
const cyclicParent = evaluate(
'/0/components/schemas/User/properties/parent/properties/parent',
'/0/components/parameters/param1/examples/example1/examples',
dereferenced,
);

Expand Down

0 comments on commit 77a6823

Please sign in to comment.