swagger-api / swagger-api/swagger-codegen

Model definitions with allOf

Open
#2,486 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature: Composition / Inheritance Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

It's seems that model compositions in Swagger CodeGen doe not work well.

For example, we can take a model from https://github.com/Azure/azure-rest-api-specs/blob/master/arm-network/2016-03-30/swagger/network.json :

"ApplicationGatewayIPConfiguration": {
      "properties": {
        "properties": {
          "x-ms-client-flatten": true,
          "$ref": "#/definitions/ApplicationGatewayIPConfigurationPropertiesFormat"
        },
        "name": {
          "type": "string",
          "description": "Gets name of the resource that is unique within a resource group. This name can be used to access the resource"
        },
        "etag": {
          "type": "string",
          "description": "A unique read-only string that changes whenever the resource is updated"
        }
      },
      "allOf": [
        {
          "$ref": "#/definitions/SubResource"
        }
      ],
      "description": "IP configuration of application gateway"
    }

Swagger CodeGen will generate following incorrect code for Java :

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-04-01T18:37:32.629+02:00")
public class ApplicationGatewayIPConfiguration extends SubResource  {

  private String id = null;

  /**
   * Resource Id
   **/

  @ApiModelProperty(value = "Resource Id")
  @JsonProperty("id")
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }

/// Methodes 
/// ...

And if we will use model definition below :

     "ApplicationGatewayIPConfiguration": {
            "allOf": [{
                "$ref": "#/definitions/SubResource"
            },
            {
                "type": "object",
                "properties": {
                    "properties": {
                        "$ref": "#/definitions/ApplicationGatewayIPConfigurationPropertiesFormat"
                    },
                    "name": {
                        "type": "string",
                        "description": "Gets name of the resource that is unique within a resource group. This name can be used to access the resource"
                    },
                    "etag": {
                        "type": "string",
                        "description": "A unique read-only string that changes whenever the resource is updated"
                    }
                }
            }],
            "description": "IP configuration of application gateway"
        }

We will get the expected result :

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-04-01T18:40:11.781+02:00")
public class ApplicationGatewayIPConfiguration extends SubResource  {

  private String name = null;
  private String etag = null;
  private String id = null;
  private ApplicationGatewayIPConfigurationPropertiesFormat properties = null;


  /**
   * Gets name of the resource that is unique within a resource group. This name can be used to access the resource
   **/

  @ApiModelProperty(value = "Gets name of the resource that is unique within a resource group. This name can be used to access the resource")
  @JsonProperty("name")
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

I have opened an issue in OpenAPI-Specification repository as well to get more information #https://github.com/OAI/OpenAPI-Specification/issues/619

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing Java generation with the two ApplicationGatewayIPConfiguration schemas shown in the issue and compare the resulting model properties. Trace how the Java generator handles allOf when properties are declared alongside it, then verify that the generated model includes the inherited id and the name, etag, and properties fields shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.