ng-openapi-gen icon indicating copy to clipboard operation
ng-openapi-gen copied to clipboard

OA 3.1. support

Open marko92milinkovic opened this issue 1 year ago • 11 comments

Does ng-openapi-gen support OA 3.1?

marko92milinkovic avatar Jan 04 '24 11:01 marko92milinkovic

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?

marko92milinkovic avatar Jan 04 '24 14:01 marko92milinkovic

This should work:

type: string
nullable: true

juneidy avatar Jan 05 '24 01:01 juneidy

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.

marko92milinkovic avatar Jan 05 '24 13:01 marko92milinkovic

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.

cgearing avatar Jan 10 '24 10:01 cgearing

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

womblep avatar Jun 04 '24 00:06 womblep

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.

womblep avatar Jun 04 '24 06:06 womblep

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 avatar Aug 26 '24 11:08 vosecek

@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 avatar Aug 26 '24 12:08 womblep

@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;
    }

vosecek avatar Aug 26 '24 12:08 vosecek

Any chances for this to be fixed in the upstream?

er1z avatar Aug 29 '24 21:08 er1z

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.

luisfpg avatar Sep 06 '24 17:09 luisfpg