Skip to content

Child in hierarchy of more than one level doesn't inherit from its parent #3474

Closed

Description

@fiore-adrian
Description

When you create a hierarchy with more than one level the generated java code of the child doesn't inherit (extends) from its parent

Swagger-codegen version

2.2.0

Swagger declaration file example
swagger: '2.0'
info:
  title: Enum Test
  description: Test enum inside array
  version: "1.0.0"
host: localhost:8080
schemes:
  - http
  - https
consumes:
  - "application/json"
produces:
  - "application/json"

paths: {}

definitions:

  example:
    type: object
    discriminator: example_type
    properties:
      id:
        type: string
      name:
        type: string
      example_type:
        type: string
        enum:
        - subexample
        - subsubexample
  subexample:
    type: object
    allOf:
    - $ref: '#/definitions/example'
    - type: object
  subsubexample:
    type: object
    allOf:
    - $ref: '#/definitions/subexample'
    - type: object
Hierarchy generated classes
public class ExampleModel {
    // ...
}
public class SubexampleModel extends ExampleModel {
    // ...
}
public class SubsubexampleModel {
    // ...
}
Error detected

SubsubexampleModel should extends from SubexampleModel

Suggested fix

First of all the parent property in a ComposedModel isn't set anymore in the parser. That's because of this issue and commit:
swagger-api/swagger-parser#245
swagger-api/swagger-parser@be6f068

Now you define the parent in swagger-codegen inside DefaultCodegen.fromModel method.
You set as the parent the first "interface" that has a discriminator. But in the case of a hierarchy with more than one level the discriminator would be in the root of the hierarchy and this condition would be false.
I change the code to first search for a parent with discriminator and if it does not exist, then I set as the parent the first of the ComposedModels inside the "interfaces".
I will add a pull request with my solution.

Thanks!
Regards,

Adrian

Activity

  1. added a commit that references this issue on Jul 28, 2016
    d460a02
  2. modified the milestones: v2.2.1, v2.2.2 on Aug 1, 2016
  3. pixelshaded commented on Aug 24, 2016

    @pixelshaded
    Contributor

    I am also getting this for typescript-node and typescript-angular. I'm guessing these changes to swagger-parser will affect every language since the responsibility of finding the parent has changed.

  4. wing328 commented on Oct 3, 2016

    @wing328
    Contributor

    PR (#3637) merged into master. Please pull the latest master to give it a try.

  5. just-jeb commented on Oct 19, 2016

    @just-jeb

    Works fine when all the models are defined in a single file.
    Still doesn't work with models split to several files. Consider the following scenario: there are multiple web services that share common resources. So there is a common models spec file, and a spec file for each service.
    When the common specs file defined like this

    {
    "definitions": {
    "ResourceDescriptor": {
        "description": "This object is a Resource Id",
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "resourceType": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "resourceType"
        ]
      },
      "BaseResource": {
        "discriminator": "baseResType",
        "type": "object",
        "description": "This is base class for all resources",
        "properties": {
          "resourceDescriptor": {
            "$ref": "#/definitions/ResourceDescriptor"
          },
          "language": {
            "type": "string"
          },
          "baseResType": {
            "type": "string"
          }
        },
        "required": [
          "resourceDescriptor",
          "baseResType"
        ]
      },
      "Outcome": {
        "type": "object",
        "properties": {
          "responseString": {
            "type": "string"
          },
          "responseCode": {
            "type": "integer"
          },
          "resourceDescriptor": {
            "$ref": "#/definitions/ResourceDescriptor"
          }
        }
      },
      "Observation": {
        "allOf": [
          {
            "$ref": "#/definitions/BaseResource"
          },
          {
            "type": "object",
            "properties": {
              "subject": {
                "$ref": "#/definitions/ResourceDescriptor"
              },
              "effective": {
                "type": "string",
                "format": "date-time"
              }
            },
            "required": [
              "subject",
              "effective"
            ]
          }
        ]
      }
      }
     }

    and in service file there is an inheritance from Observation (see the attachment) the code that is produced (with Java language) doesn't contain any extends. Neither for Observation (which resides in the same file with its parent) or MyCoolResource (which extends Observation and sits in a different file).
    Here is even weirder thing:
    If you do define an additional discriminator for Observation like this:

    "Observation": {
        "allOf": [
          {
            "$ref": "#/definitions/BaseResource"
          },
          {
            "discriminator": "observationType",
            "type": "object",
            "properties": {
              "subject": {
                "$ref": "#/definitions/ResourceDescriptor"
              },
              "effective": {
                "type": "string",
                "format": "date-time"
              },
              "observationType": {
                "type": "string"
              }
            },
            "required": [
              "subject",
              "effective",
              "observationType"
            ]
          }
        ]
      }

    the code that is produced for MyCoolResource does contain extends! Which is completely fine by me, I wouldn't mind adding another discriminator.
    But the code that is produced for Observation still lacks extends of BaseResource

    ModelsForGithub.json.txt
    ServiceForGithub.json.txt

  6. sreeshas commented on Dec 4, 2016

    @sreeshas
    Contributor

    I'm little confused here. Can someone clarify my doubts?

    My understanding is a subclass extends from baseclass if baseclass has declared a discriminator. Otherwise subclass just composes fields from baseclass with no parent child relationship.

    example can be used in polymorphic way, because it declares a discriminator.
    subexample "extends" from example ( because example can be used in a polymorphic way)

    Shouldnt' subsubexample extend example because example was the one who defined the discriminator?
    if subsubexample extends subexample, doesn't it mean subexample can be used in a polymorphic way (which was not intended)?
    Isn't subsubexample "composing" from subexample, because subexample has not declared a discriminator ?

  7. jeff9finger commented on Dec 8, 2016

    @jeff9finger

    It is my understanding that when allOf: references another definition that does not contain a discriminator, that the Java class will "extend" the other definition, but there will not be Jackson annotations on the classes that will allow correct serialization and deserialization.

    The discriminator in a definition enable the generation of the Jackson annotations on the Java classes.

    Using the original example:

    definitions:
    
      example:
        type: object
        discriminator: example_type
        properties:
          id:
            type: string
          name:
            type: string
          example_type:
            type: string
            enum:
            - subexample
            - subsubexample
      subexample:
        type: object
        allOf:
        - $ref: '#/definitions/example'
        - type: object
      subsubexample:
        type: object
        allOf:
        - $ref: '#/definitions/subexample'
        - type: object
    

    In order to create Java classes that serialize/deserialize subsubexample as a subexample type, a unique discriminator must be declared on subexample.

      subexample:
        type: object
        allOf:
        - $ref: '#/definitions/example'
        - type: object
           discriminator: sub_example_type
           required:
           - sub_example_type
           properties:
             sub_example_type:
               type: string
           ...
    
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