Skip to content

If allOf used in a base type, inherited types are missing "extends <basetype>" in generated Java code #3544

Closed

Description

@ggood
Description

When a type is constructed using allOf, and also includes a discriminator property (so it can serve as the base class for inherited types) the inherited types do not extend from it.

If instead, the "allOf" construct is collapsed and all the properties inlined, the inheritance works as expected.

In the example below, the generated code for LevelTwoTypeOne is missing the "extends LevelOne", while the generated code for LevelTwoTypeTwo includes "extends InlinedLevelOne" as expected.

Swagger-codegen version

2.2.0

Swagger declaration file content or url
---
swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
paths:
  /:
    get:
      responses:
        200:
          description: OK
definitions:
  Base:
    type: object
    properties:
      name:
        type: string

  LevelOne:
    allOf:
    - $ref: '#/definitions/Base'
    - type: object
      discriminator: resource_type
      properties:
        color:
          type: string
        resource_type:
          type: string
      required: [resource_type]

  InlinedLevelOne:
    type: object
    discriminator: resource_type
    properties:
      name:
        type: string
      color:
        type: string
      resource_type:
        type: string
    required: [resource_type]

  LevelTwoTypeOne:  
    type: object
    allOf:
    - $ref: '#/definitions/LevelOne'
    - type: object
      properties:
        odor:
          type: string

  LevelTwoTypeTwo:  
    type: object
    allOf:
    - $ref: '#/definitions/InlinedLevelOne'
    - type: object
      properties:
        height:
          type: string
    ```

##### Command line used for generation

export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/inheritance.yaml -l java --language-specific-primitives integer,Long -o samples/client/nsx/java/default -DhideGenerationTimestamp=true"

java $JAVA_OPTS -jar $executable $ags

##### Steps to reproduce

After running swagger-codegen, I observe:

LevelTwoTypeOne.java's class declaration is:

public class LevelTwoTypeOne   {

I expected it to inherit from LevelOne.

LevelTwoTypeTwo's class declaration is:

public class LevelTwoTypeTwo extends InlinedLevelOne  {

as expected.

##### Related issues

Sounds a bit similar to https://github.com/swagger-api/swagger-codegen/issues/3474. However, I hand-applied the related fix (https://github.com/medallia/swagger-codegen/commit/d460a024ef5b54c94b7c10a28ea98679c532a75e) and it didn't change the behavior.

##### Suggest a Fix

 Something about using allOf to describe a base class seems to be the problem.

Activity

  1. modified the milestone: v2.2.1 on Aug 8, 2016
  2. asafbennatan commented on Aug 13, 2016

    @asafbennatan

    in an older version that worked (version 2.0) , but subclasses had the fields of supertype as well as their own,
    my guess is that that bug fix went wrong,
    P.S
    that happens in android client as well

  3. added this to the v2.2.2 milestone on Aug 15, 2016
  4. wing328 commented on Aug 15, 2016

    @wing328
    Contributor

    @cbornet when you've time, I wonder if you can take a look.

  5. cbornet commented on Aug 23, 2016

    @cbornet
    Contributor

    It looks like the same as #3474 : we don't look in for the discriminator in allOf recursively. I'm working on something.

  6. wing328 commented on Oct 3, 2016

    @wing328
    Contributor

    @ggood PR merged into master. Please pull the latest master to give it a try.

  7. ggood commented on Nov 30, 2016

    @ggood
    ContributorAuthor

    Sorry for not responding sooner - this addresses the issue for me. Thanks for the fix.

  8. karux commented on Jan 23, 2017

    @karux

    does the project have a test exercising this use case?

  9. wing328 commented on Jan 24, 2017

    @wing328
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions