-
Notifications
You must be signed in to change notification settings - Fork 14
Add converter for oneOf/anyOf with nullable types #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattoni
wants to merge
4
commits into
apiture:main
Choose a base branch
from
mattoni:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ import { | |
| JsonNode, | ||
| RefObject, | ||
| SchemaObject, | ||
| isRef, | ||
| getRefSchema, | ||
| } from './RefVisitor'; | ||
|
|
||
| /** Lightweight OAS document top-level fields */ | ||
|
|
@@ -144,6 +146,7 @@ export class Converter { | |
| this.convertJsonSchemaContentMediaType(); | ||
| this.convertConstToEnum(); | ||
| this.convertNullableTypeArray(); | ||
| this.convertMergedNullableType(); | ||
| this.removeWebhooksObject(); | ||
| this.removeUnsupportedSchemaKeywords(); | ||
| if (this.convertSchemaComments) { | ||
|
|
@@ -244,6 +247,78 @@ export class Converter { | |
| visitSchemaObjects(this.openapi30, schemaVisitor); | ||
| } | ||
|
|
||
| /** | ||
| * Converts `type: null` merged with other types via anyOf/oneOf to `nullable: true` | ||
| */ | ||
| convertMergedNullableType() { | ||
| const schemaVisitor: SchemaVisitor = (schema: SchemaObject): SchemaObject => { | ||
| const nullableOf = ['anyOf', 'oneOf'] as const; | ||
|
|
||
| nullableOf.forEach((of) => { | ||
| if (!schema[of]) { | ||
| return; | ||
| } | ||
|
|
||
| const entries = schema[of]; | ||
|
|
||
| if (!Array.isArray(entries)) { | ||
| return; | ||
| } | ||
|
|
||
| const typeNullIndex = entries.findIndex((v) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would be cleaner with |
||
| if (!v) { | ||
| return false; | ||
| } | ||
| return v.hasOwnProperty('type') && v['type'] === 'null'; | ||
| }); | ||
|
|
||
| let isNullable = typeNullIndex > -1; | ||
|
|
||
| // Get the main type of the root object. nullable can't exist with the type property in 3.0.3. | ||
| const mainType = entries.reduce((acc, cur) => { | ||
| const sub = isRef(cur) ? getRefSchema(this.openapi30, cur) : cur; | ||
|
|
||
| if (sub['type']) { | ||
| // if our sub-type...type has null in it, it's still nullable | ||
| if (Array.isArray(sub['type'])) { | ||
| if (sub['type'].includes('null')) { | ||
| isNullable = true; | ||
| } | ||
| // return the first non-null type. multiple unrelated types aren't | ||
| // currently supported. | ||
| return sub['type'].filter((_) => _ !== 'null')[0]; | ||
| } | ||
| // only set non null types | ||
| if (sub['type'] !== 'null') { | ||
| return sub['type']; | ||
| } | ||
| } | ||
| return acc; | ||
| }, null); | ||
|
|
||
| if (isNullable) { | ||
| // has a type: null entry in the array | ||
| schema['nullable'] = true; | ||
| schema['type'] = mainType; | ||
| if (typeNullIndex > -1) { | ||
| schema[of].splice(typeNullIndex, 1); | ||
| } | ||
|
|
||
| if (entries.length === 1) { | ||
| // if only one entry, anyOf/oneOf probably shouldn't be used. | ||
| // Instead, convert to allOf with nullable & ref | ||
| schema['allOf'] = [schema[of][0]]; | ||
|
|
||
| delete schema[of]; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return this.walkNestedSchemaObjects(schema, schemaVisitor); | ||
| }; | ||
| visitSchemaObjects(this.openapi30, schemaVisitor); | ||
| } | ||
|
|
||
| removeWebhooksObject() { | ||
| if (Object.hasOwnProperty.call(this.openapi30, 'webhooks')) { | ||
| this.log(`Deleted webhooks object`); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a safe assumption; i.e. the $ref could be a reference to a schema in another file, such as
$ref: '../components.yaml/#/components/schemas/foo'in which case fetchingschemas[propertyName]may fail or may fetch the wrong schema.This tool does not resolve external references; see the README which says
"The tool only supports self-contained documents. It does not follow or resolve external
$refdocuments embedded in the source document."See https://github.com/apiture/api-ref-resolver for tooling to supports resolving external
$refreferences before down converting. (However this tool should not assume that all external$refhave been resolved)So instead, I suggest either checking that the $ref starts with '#/components/schemas
or after splitting on '/' thatcomponents[0] === '#' && components[1] === 'components' && components[2] === 'schemas'`