OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG] allOf used to add constraints for string properties results in faulty generated DTO class if property is set multiple times #17409

Open blakharaz opened 11 months ago

blakharaz commented 11 months ago

Bug Report Checklist

Description

schemas with allOf definitions setting the same property multiple times result in a generated type with no property at all

openapi-generator version

I used commit hash 1b2917d69f13ea0545f755a6e2963923ff2fb367 and the "csharp" generator as well as a new generator for a different target language I'm developing right now.

OpenAPI declaration file content or url
{
    "openapi": "3.0.3",
    "info": {
      "title": "allOf used to add constraints bug",
      "description": "allOf used to add constraints for string properties results in faulty generated DTO class",      
      "version": "1b2917d69f13ea0545f755a6e2963923ff2fb367"
    },
    "paths": {
        "/actual": {
            "get": {
                "responses": {
                    "200": {
                        "description": "reproduces the bug",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Actual"
                                }
                            }
                        }
                    }
                }                
            }
        },
        "/expected": {
            "get": {
                "responses": {
                    "200": {
                        "description": "returns expected type",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Expected"
                                }
                            }
                        }
                    }
                }                
            }
        }
    },
    "components": {
        "schemas": {
            "Actual": {
                "properties": {
                    "myString": {
                        "allOf": [
                            {"type": "string"},
                            {"pattern": "[a-z]*"},
                            {"pattern": "[a-zA-Z]*"}
                        ]
                    }
                }
            },
            "Expected": {
                "properties": {
                    "myString": {
                        "type": "string",
                        "pattern": "[a-z]*"
                    }
                }
            }
        }
    }
}
Generation Details

My settings for generator as code:

        final CodegenConfigurator configurator = new CodegenConfigurator()
                .setGeneratorName("csharp")
                .setApiPackage("Rest")
                .setModelPackage("Rest.Dto")
                .setOpenAPINormalizer(Map.of("REFACTOR_ALLOF_WITH_PROPERTIES_ONLY", "true"))
                .setInputSpec("minimal_config_for_allOf_bug.json")
                .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

        // when
        final ClientOptInput clientOptInput = configurator.toClientOptInput();
        DefaultGenerator generator = new DefaultGenerator();
        Map<String, File> files = generator.opts(clientOptInput).generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
Steps to reproduce
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/10010

Suggest a fix

In case of allOf properties of the same property should be merged (taking the first or the last if set multiple times, the spec doesn't state anything for that case), that way there would be no need to generate a new type, especially for basic type properties.

blakharaz commented 11 months ago

Having one reference and one inline schema in the allOf also doesn't generate code I would expect. The generated type for the property (which is of type string so I wouldn't even expect an additional type here) contains a property of type object instead of string.

Specification for this:

{
    "openapi": "3.0.3",
    "info": {
      "title": "allOf used to add constraints bug",
      "description": "allOf used to add constraints for string properties results in faulty generated DTO class",      
      "version": "1b2917d69f13ea0545f755a6e2963923ff2fb367"
    },
    "paths": {
        "/actual": {
            "get": {
                "responses": {
                    "200": {
                        "description": "reproduces the bug",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Actual"
                                }
                            }
                        }
                    }
                }                
            }
        },
        "/expected": {
            "get": {
                "responses": {
                    "200": {
                        "description": "returns expected type",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Expected"
                                }
                            }
                        }
                    }
                }                
            }
        }
    },
    "components": {
        "schemas": {
            "Actual": {
                "properties": {
                    "myString": {
                        "allOf": [
                            {"type": "string", "pattern": "[a-z]*"},
                            {"$ref": "#/components/schemas/BaseClass"}
                        ]
                    }
                }
            },
            "BaseClass": {
                "properties": {
                    "myString": {
                        "pattern": "[a-z]*"
                    }
                }
            },
            "Expected": {
                "properties": {
                    "myString": {
                        "type": "string",
                        "pattern": "[a-z]*"
                    }
                }
            }
        }
    }
}