-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolver): add support for mode resolver option
This option activates allOf plugin/visitor. Refs #2750
- Loading branch information
Showing
11 changed files
with
1,095 additions
and
112 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ers/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/visitors/all-of.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { isArrayElement, deepmerge } from '@swagger-api/apidom-core'; | ||
import { isSchemaElement, SchemaElement } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
|
||
const AllOfVisitor = () => ({ | ||
SchemaElement: { | ||
leave(schemaElement) { | ||
// do nothing | ||
if (typeof schemaElement.allOf === 'undefined') return undefined; | ||
// throw if allOf keyword is not an array | ||
if (!isArrayElement(schemaElement.allOf)) { | ||
throw new TypeError('allOf must be an array'); | ||
} | ||
// remove allOf keyword if empty | ||
if (schemaElement.allOf.isEmpty) { | ||
return new SchemaElement( | ||
schemaElement.content.filter((memberElement) => memberElement.key.toValue() !== 'allOf'), | ||
schemaElement.meta.clone(), | ||
schemaElement.attributes.clone() | ||
); | ||
} | ||
// throw if allOf keyword contains anything else than Schema Object | ||
schemaElement.allOf.forEach((item) => { | ||
if (!isSchemaElement(item)) { | ||
throw new TypeError('Elements in allOf must be objects'); | ||
} | ||
}); | ||
|
||
const mergedSchemaElement = deepmerge.all([...schemaElement.allOf.content, schemaElement]); | ||
|
||
/** | ||
* If there was not an original $$ref value, make sure to remove | ||
* any $$ref value that may exist from the result of `allOf` merges. | ||
*/ | ||
if (!schemaElement.hasKey('$$ref')) { | ||
mergedSchemaElement.remove('$$ref'); | ||
} | ||
|
||
/** | ||
* If there was an example keyword in the original definition, | ||
* keep it instead of merging with example from other schema. | ||
*/ | ||
if (schemaElement.hasKey('example')) { | ||
const member = mergedSchemaElement.getMember('example'); | ||
member.value = schemaElement.get('example'); | ||
} | ||
|
||
/** | ||
* If there was an examples keyword in the original definition, | ||
* keep it instead of merging with examples from other schema. | ||
*/ | ||
if (schemaElement.hasKey('examples')) { | ||
const member = mergedSchemaElement.getMember('examples'); | ||
member.value = schemaElement.get('examples'); | ||
} | ||
|
||
// remove allOf keyword after the merge | ||
mergedSchemaElement.remove('allOf'); | ||
return mergedSchemaElement; | ||
}, | ||
}, | ||
}); | ||
|
||
export default AllOfVisitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.