Skip to content
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

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

Open
5 of 6 tasks
blakharaz opened this issue Dec 15, 2023 · 1 comment

Comments

@blakharaz
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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 1b2917d 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

#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
Copy link
Author

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]*"
                    }
                }
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant