1

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

BlaZz91
  • 11
  • 3

1 Answers1

1

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

Hollerweger
  • 975
  • 1
  • 13
  • 32