-
-
Notifications
You must be signed in to change notification settings - Fork 390
Support v3.1 typed arrays #1167
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
base: main
Are you sure you want to change the base?
Conversation
In OpenAPI v3.1 the nullable property was replaced with a "typed array", that is an array that can either be of type T or null. To make the minimal amount of changes this commit converts a type array back into what a 3.0 parser would expect. Closes acacode#991
|
@smorimoto any chance you could review this? We could really use this change. |
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.
Pull Request Overview
This PR introduces support for OpenAPI v3.1 typed arrays by converting them back to the v3.0 format to minimize changes.
- Adjusts the schema parser to detect a two-element type array containing "null".
- Sets the type and nullable properties accordingly.
if (property.type[0] == "null") { | ||
property.type = property.type[1]; | ||
property.nullable = true; | ||
} else if (property.type[1] == "null") { |
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.
Consider using strict equality (===) instead of loose equality (==) for comparing property.type elements to "null" to avoid potential type coercion issues.
if (property.type[0] == "null") { | |
property.type = property.type[1]; | |
property.nullable = true; | |
} else if (property.type[1] == "null") { | |
if (property.type[0] === "null") { | |
property.type = property.type[1]; | |
property.nullable = true; | |
} else if (property.type[1] === "null") { |
Copilot uses AI. Check for mistakes.
property.type = property.type[0]; | ||
property.nullable = true; | ||
} | ||
} |
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.
I had to add also this after your condition, to type array with single type work.
if (Array.isArray(property.type) && property.type.length == 1) { property.type = property.type[0]; }
Example yaml:
SomeType:
type:
- string
enum:
- one
- two
OtherComponent:
type: object
properties:
type:
$ref: '#/components/schemas/SomeType'
In OpenAPI v3.1 the nullable property was replaced with a "typed array", that is an array that can either be of type T or null.
To make the minimal amount of changes this commit converts a type array back into what a 3.0 parser would expect.
Closes #991