openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[Java] allOf with multiple values is not handled properly

Open zslajosfalvi opened this issue 7 years ago • 8 comments

I'm using 4.0.0 version with Maven plugin to generate jaxrs-spec.

<configuration>
  <generateModelTests>false</generateModelTests>
  <generateApiTests>false</generateApiTests>
  <generateSupportingFiles>false</generateSupportingFiles>
  <inputSpec>${project.build.directory}/classes/sample.yaml</inputSpec>
  <skipValidateSpec>true</skipValidateSpec>
  <generatorName>jaxrs-spec</generatorName>
  <output>${project.build.directory}/generated-sources</output>
  <configOptions>
    <sourceFolder>java</sourceFolder>
    <apiPackage>${api.package}</apiPackage>
    <modelPackage>${model.package}</modelPackage>
    <useGenericResponse>true</useGenericResponse>
    <useBeanValidation>false</useBeanValidation>
    <interfaceOnly>true</interfaceOnly>
    <serializableModel>true</serializableModel>
  </configOptions>
</configuration>
components:
  schemas:
    BaseRequest:
      type: object
      properties:
        sampleField:
          type: string

    Payment:
      type: object
      properties:
        paymentSampleField:
          type: string

    CreditTransferRequest:
      allOf:
        - $ref: '#/components/schemas/BaseRequest'
        - $ref: '#/components/schemas/Payment'

The model generate doesn't use inheritance.

Generated Java class for CreditTransferRequest is:

...
public class CreditTransferRequest  implements Serializable {
  
  private  String sampleField;
  private  String paymentSampleField;
...

BaseRequest class gets generated, but is not used for inheritance.

I'm not sure if this a bug or not but I expect to have a generated class like

...
public class CreditTransferRequest extends BaseRequest implements Serializable {
  
  private  String paymentSampleField;
...

zslajosfalvi avatar May 14 '19 07:05 zslajosfalvi

I've got a similar behavior with the @typescript-angular@ generator using the cli generator

dbusacca avatar May 22 '19 13:05 dbusacca

same for typescript-node

raffis avatar Jun 13 '19 15:06 raffis

same for jaxrs-jersey. This problem did not exist in version 3.3.4. @wing328 any chances for a fix in next release?

res13 avatar Dec 11 '19 11:12 res13

Sorry guys if I'm bother you with such problems, but I think this should be a very basic and fundamental feauture of the code generator. Without it I can't generate proper Object definitions. Currently I have to stick with version 3.3.4 because non of the new versions fixed this problem nor the issue #2058 or any other issues I created and are still open. Is there any chance to get fixed this problems or should I look for an other generator?

zslajosfalvi avatar Dec 14 '19 10:12 zslajosfalvi

Ouch, same problem here with Quarkus. Tried 4.2.3, no luck. Tried 5.1.1, no luck. 3.3.x is too old to have the Quarkus generator.

InfoSec812 avatar Aug 16 '22 14:08 InfoSec812

OK, found a workaround. I had to revert to version 4.2.1 and modify my spec file like this for the inheriting type:

components:
  schemas:
    EmployeeEntity:
      type: object
      allOf:
        - $ref: "#/components/schemas/Employee"
      properties:
        assignments:
          description: Projects for which the employee has been assigned to a SurveyGroup
          items:
            $ref: "#/components/schemas/EmployeeAssignment"
          type: array

So, instead of having the allOf at the top level of the component schema, it was added as part of the object which does not match the examples from the specification standards HERE

InfoSec812 avatar Aug 16 '22 14:08 InfoSec812

I was mistaken... The properties after the allOf get left out. I tried to use something like this, but then it stops doing inheritance.

components:
  schemas:
    EmployeeEntity:
      type: object
      allOf:
        - $ref: "#/components/schemas/Employee"
        - type: object
          properties:
            assignments:
              description: Projects for which the employee has been assigned to a SurveyGroup
              items:
                $ref: "#/components/schemas/EmployeeAssignment"
              type: array

InfoSec812 avatar Aug 16 '22 14:08 InfoSec812

please consider using openapi normalizer with the rule REF_AS_PARENT_IN_ALLOF enabled: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#openapi-normalizer

wing328 avatar Dec 03 '23 10:12 wing328