4

Since springdoc-openapi-ui version 1.4.0, I am not able to manage java pojo inheritance anymore. I know that the concept of AllOf, OneOf has been added in 1.4.0 but I can't figure out how to make it work.

I have a simple pojo that contains a list of X (x is abstract). There's 2 possible implementations. Proper implementation is determine with an attribute of class X.

** Code: (class names has been renamed) **

CheeseDTO YAML in both version :

   CheeseDTO:
     type: object
     properties:
       cheeseType:
         type: string
     discriminator:
       propertyName: cheeseType    

With springdoc-openapi-ui 1.3.9, my yaml is generated like this:

   MyDTO:
       type: object
       properties:
           cheeses:
           type: array
           items:
               $ref: '#/components/schemas/CheeseDTO'

Generated DTO via open openapi-generator-maven-plugin 4.3.0

private List<CheeseDTO> cheeses = null;

With springdoc-openapi-ui 1.5.4, my yaml is generated like this:

MyDTO:
       type: object
       properties:
           cheeses:
           type: array
           items:
               oneOf:
               - $ref: '#/components/schemas/SoftCheeseDTO'
               - $ref: '#/components/schemas/HardCheeseDTO'    

Generated DTO via open openapi-generator-maven-plugin 4.3.0 (This is my issue MyDTOCheesesOneOf instead of CheeseDTO)

private List<MyDTOCheesesOneOf> cheeses = null;

Swagger 3 annotations :

@Schema(
name = "CheeseDTO",
discriminatorProperty = "cheeseType",
discriminatorMapping = {@DiscriminatorMapping(value = "Brie", schema = SoftCheeseDTO.class),
 @DiscriminatorMapping(value = "Banon", schema = SoftCheeseDTO.class),
 @DiscriminatorMapping(value = "Cheddar", schema = HardCheeseDTO.class)})
abstract CheeseDTO

   private String cheeseType;
@Schema(allOf = {CheeseDTO.class})
SoftCheeseDTO extends CheeseDTO
@Schema(allOf = {CheeseDTO.class})
HardCheeseDTO extends CheeseDTO

OpenAPi Generator maven plugin

<plugin>
       <groupId>org.openapitools</groupId>
       <artifactId>openapi-generator-maven-plugin</artifactId>
       <version>4.3.0</version>
       <executions>
         <execution>
           <id>generateWebQuoteApiClient</id>
           <goals>
             <goal>generate</goal>
           </goals>
           <configuration>
             <inputSpec>/definitions/webQuoteApi.yaml</inputSpec>
             <generatorName>java</generatorName>
             <generateApiDocumentation>false</generateApiDocumentation>
             <configOptions>
               <library>jersey2</library>
               <dateLibrary>java8</dateLibrary>
               <java8>true</java8>
               <modelPackage>${client.package}.model</modelPackage>
               <apiPackage>${client.package}.api</apiPackage>
               <invokerPackage>${client.package}.api</invokerPackage>
               <performBeanValidation>false</performBeanValidation>
               <serializationLibrary>jackson</serializationLibrary>
             </configOptions>
           </configuration>
         </execution>
       </executions>
     </plugin>

Is there a way to generate a List<CheeseDTO> with springdoc-openapi-ui > 1.4.0 ? Do i have to change my swagger annotations or change my java generator ?

** I tried update the generator plugin to the latest version but had the same results

Thanks for any help David

1
  • anyone can help? Commented Mar 2, 2021 at 14:30

1 Answer 1

3

We see same issue with newer sprindoc openapi ui. You need to stick with springdoc-openapi-ui 1.3.9.

Similar issue is on generator:

Until openapi-generator-maven-plugin 4.3.1 you can do it following way:

CheeseDTO:
  type: object
  properties:
    cheeseType:
      type: string
  discriminator:
    propertyName: cheeseType
    mapping:
      SOFT_CHEESE: '#/components/schemas/SoftCheeseDTO'
      HARD_CHEESE: '#/components/schemas/HardCheeseDTO'

And in your API return CheeseDTO:

MyDTO:
  type: object
  properties:
    cheeses:
      type: array
        items:
          $ref: '#/components/schemas/CheeseDTO'

This should correctly generate List<CheeseDTO>.

With newer openapi-generator-maven-plugin 5.x this is not working anymore as propertyName is not supported anymore and oneOf produces wrong inheritance with List<MyDTOCheesesOneOf>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.