openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

OpenAPI V3 CompositionExtendedModel example incorrect and behaviour incorrect.

Open gthomas2 opened this issue 3 years ago • 2 comments

Referring to the following documentation, a model extending another model should place its own properties within an entry of the "allOf" field. https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

In the above documentation, the field "rootCause" is within allOf > properties.

Comparing this with the v3.json file used for testing, it is evident that the tool expects properties to live outside of "allOf".

        "CompositionExtendedModel": {
            "description": "This is a model that extends the base model",
            "type": "object",
            "allOf": [
                {
                    "$ref": "#/components/schemas/CompositionBaseModel"
                }
            ],
            "properties": {
                "age": {
                    "type": "number"
                }
            },
            "required": [
                "firstName",
                "lastname",
                "age"
            ]
        }

If I try running OpenAPI V3 json or yml through the tool it will not include the properties in the extended model if the properties for the sub model are included within an entry of the allOf field.

gthomas2 avatar May 11 '22 14:05 gthomas2

I'm having the same problem. Did you find a workaround ?

AllanPinheiroDeLima avatar May 23 '22 01:05 AllanPinheiroDeLima

This got it working for me but some of the unit tests fail as a result which means you should probably be very careful if you decide to try and use this patch:

--- a/src/openApi/v3/parser/getModelComposition.ts
+++ b/src/openApi/v3/parser/getModelComposition.ts
@@ -55,6 +55,16 @@ export const getModelComposition = (
         properties.push(...requiredProperties);
     }
 
+    if (definition.allOf?.length) {
+        definition.allOf.forEach((entry) => {
+            if (entry?.properties) {
+                definition.properties = {...definition.properties, ...entry.properties};
+            }
+        })
+    }

gthomas2 avatar May 23 '22 09:05 gthomas2