ng-openapi-gen
ng-openapi-gen copied to clipboard
OA 3.1. support
Does ng-openapi-gen support OA 3.1?
I have impression that it can't work with nullability feature in OpenApi 3.1 which is described as array:
"schemas": {
"SomeResource": {
"description": "",
"properties": {
"someProperty": {
"type": [
"string",
"null"
]
},
The problematic code seems to be inside collectObject(schema, propertiesByName)
function.
Could you please check if the library can work with this nullability notation? If not, is there a plan to support this feature from OA 3.1?
This should work:
type: string
nullable: true
Hi @juneidy , You're correct. The following notation is indeed valid in OpenAPI 3.0:
type: string
nullable: true
However, OpenAPI 3.1 introduces an alternative format to specify the type property as an array:
"type": ["string", "null"]
This is the format my library (NelmioApiDocBundle) uses for generating API specifications compliant with OpenAPI 3.1.0. The choice of library, though, isn't particularly relevant to the discussion.
From our testing, it appears that ng-openapi-gen hasn't yet adopted this new syntax. Does anyone know if or when ng-openapi-gen plans to support this format? Thanks for any insights.
It also doesn't appear to support const
as a way of indicating that a property can only be a single value and cannot be changed.
This issue looks to be with this code:
const appendType = (type: string) => {
if (type.startsWith('null | ')) {
propTypes.add('null');
propTypes.add(type.substring('null | '.length));
} else {
propTypes.add(type);
}
};
If that could also have an array passed and if type is an array, check for null, then it should work
I monkey patched that code and it allows the files to be generated. The "object" handlebars template also needs to be updated to put a "|" between multiple types.
I monkey patched that code and it allows the files to be generated. The "object" handlebars template also needs to be updated to put a "|" between multiple types.
Can you provide @womblep object.handlebards updated template? Thanks.
@vosecek sorry I dont have time to do a PR at the moment, I went down a different path to solve my problem. Here is a quick patch I did. Hope it helps openapi_3_1_compatibility_with_null.patch
@womblep thanks, looks great.
I achieved meanwhile to working solution by modifying method collectContent(desc)
operation.js file, but your result looks much better.
my solution (including skip type.startsWith
in model.js
) is here.
collectContent(desc) {
const result = [];
if (desc) {
for (const type of Object.keys(desc)) {
for (var i in desc[type].schema.properties) {
if (typeof desc[type].schema.properties[i].type === 'object') {
if(typeof desc[type].schema.properties[i].type.includes('null')) {
desc[type].schema.properties[i].type = desc[type].schema.properties[i].type.toString().replace(',null','|null');
}
}
}
result.push(new content_1.Content(type, desc[type], this.options, this.openApi));
}
}
return result;
}
Any chances for this to be fixed in the upstream?
Sorry, I've been quite busy and don't have any time left for this project lately. From time to time I do a push in this project, but November 2023 was the last time I could touch it. I do have in mind the OAS 3.1 changes, but I can't promise any time frame.