Skip to content

[Java] Useless generation of fields when using allOf #1348

Closed

Description

@cbornet

When using composition with allOf, the child class contains un-needed attributes which are already in the parent class.

Activity

  1. cbornet commented on Oct 7, 2015

    @cbornet
    ContributorAuthor

    It even leads to an exception : class io.swagger.client.model.Bar declares multiple JSON fields named foo when using Gson (in okhttp-gson and retrofit libraries)

  2. wing328 commented on Oct 7, 2015

    @wing328
    Contributor

    @cbornet my understanding of allOf (without discriminator) is that child should "copy" all the parent properties to itself (child) since child does not inherit (extend) parent.

    Ref: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#composition-and-inheritance-polymorphism

    Do you have a sample spec so that I can repeat the issue you mentioned above?

  3. cbornet commented on Oct 7, 2015

    @cbornet
    ContributorAuthor

    @wing328 Well, it seems to me that implementing swagger composition with class extension would be fine if there was only one object possible in the composition since the classes are just mere containers for the Json mapping. But since there can be several objects to compose, it looks preferable to just copy the attributes for clarity and remove the class inheritance in the generated code.

  4. cbornet commented on Oct 7, 2015

    @cbornet
    ContributorAuthor

    A spec to reproduce here: https://raw.githubusercontent.com/swagger-api/swagger-spec/07cce43535e90815737e7258bb4d51c0c76999d6/examples/v2.0/json/petstore-expanded.json

    public class Pet extends NewPet  {
    
      @SerializedName("name")
      private String name = null;
    
      @SerializedName("tag")
      private String tag = null;
    
      @SerializedName("id")
      private Long id = null;
    public class NewPet   {
    
      @SerializedName("name")
      private String name = null;
    
      @SerializedName("tag")
      private String tag = null;
  5. cbornet commented on Oct 10, 2015

    @cbornet
    ContributorAuthor

    The issue is also debated in #1120 . We need to have class extension or copy but not both as it fails with gson.

  6. added a commit that references this issue on Oct 10, 2015
    f374c77
  7. added this to the v2.1.5 milestone on Oct 26, 2015
  8. cbornet commented on Nov 9, 2015

    @cbornet
    ContributorAuthor

    I have tested that a definition with multiple allOf gets the copies of all the elements so composition is working fine if we remove the class extension. As per #1120, there seems to be an issue when the parent holds an enum. I think the solution there would be to also copy the enum to the child class (or import the one from the parent class if it is visible).
    I agree with @wing328 that class extension should be reserved for when we have discriminator support.
    So what should be done ? I have removed the class extension on my fork and I can pull a PR.

  9. modified the milestones: v2.1.5, v2.1.6 on Jan 6, 2016
  10. hiveship commented on Feb 8, 2016

    @hiveship
    Contributor

    @wing328 I'm facing the problem. What is the final decision about it ?

  11. modified the milestones: v2.2.0, v2.1.6 on Mar 19, 2016
  12. ethlo commented on May 4, 2016

    @ethlo

    I was going to open a separate issue (and I still can if you find that more appropriate), but I just wanted mention other issues with model generation. It seems that model generation where inheritance/composition is involved is severely broken.

    Basically:
    It should support the same inheritance hierarchy as specified in the Swagger model.

    Also, this generates no extra properties at all (it is the same as Page class, with no extra properties):

    PageBatchOrder:
        allOf: 
        - $ref: '#/definitions/Page'
        type: object
        properties: 
          content: 
            type: array
            items: 
              $ref: '#/definitions/BatchOrder'
    

    So my question is, is this an issue that people are actively working on, or should I have a look?

  13. modified the milestones: v2.2.0, v2.3.0 on Jul 7, 2016
  14. wing328 commented on Jul 19, 2016

    @wing328
    Contributor

    @cbornet PR (#3393) has been merged into master. Please pull the latest to give it a try.

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