Open API generator allOf not generating model correctly
Asked Answered
D

1

6

I want to generate our models based on an Openapi spec 3.0 yml definition. In my spec I have a definition using allOf to include the fields of the other components. When generating the models with the openapi-generator-maven-plugin. I see the following warning allOf with multiple schemas defined. Using only the first one.

The result is that only the first allOf definition's properties are included. While I expected all fields to be included. https://editor.swagger.io/ generates the schemas correct.

Why are not all properties defined in an allOf included?

Schema definition:

Dto:
  title: Dto
  type: object
  properties:
    created_by:
      type: string
      format: date-time

Foo:
  title: Foo
  type: object
  allOf:
    - $ref: '#/Dto'
  properties:
    fooProperty:
      type: string

Bar:
  title: Bar
  type: object
  allOf:
    - $ref: '#/Foo'
  properties:
    barProperty:
      type: string

maven plugin configuration:

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>6.1.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>myspec.yml</inputSpec>
                        <generatorName>spring</generatorName>
                        <generateApis>false</generateApis>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <configOptions>
                            <serializableModel>true</serializableModel>
                            <documentationProvider>none</documentationProvider>
                            <openApiNullable>false</openApiNullable>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

The result is Bar with only the fields of Dto and Bar but not those of Foo. Why are not all properties defined in an allOf included?

Delete answered 26/9, 2022 at 11:40 Comment(2)
Did you find any answer? I am facing same issue..where generating kotlin files, it generates only allOf field, but no other properties are in generated class. Please let me know if you found a solutionReceiptor
@Receiptor github.com/OpenAPITools/openapi-generator/issues/10010 - no solution yetDelete
P
6

The way allOf is processed has recently changed, as of v6.5.0, in order to deal with the exact issue you're describing. The changes were motivated by this issue, and introduced in this PR.

The new functionality is not default behaviour, however. In order to try it, you can use the REFACTOR_ALLOF_WITH_PROPERTIES_ONLY flag. The following example is for the openapi-generator-cli, though it can easily be extended to the Maven plugin using the openApiNormalizer configuration.

openapi-generator generate -g <target_language> -i <api_spec_file> -o gen --openapi-normalizer REFACTOR_ALLOF_WITH_PROPERTIES_ONLY=true
Percussionist answered 7/6, 2023 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.