OpenAPITools / OpenAPITools/openapi-generator

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

Open
#17,409 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied minimal OpenAPI declaration and the Java CodegenConfigurator setup using the csharp generator and REFACTOR_ALLOF_WITH_PROPERTIES_ONLY. Reproduce the generated Actual and Expected DTOs, then trace allOf property handling; done means the repeated string constraints no longer remove myString from the generated type.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.