swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

Swagger ignores vendorextention for $ref type properties #2219

Open sbelan opened 8 years ago

sbelan commented 8 years ago

Hi @wing328 ,

Swagger ignores vendorextention for $ref type properties. For e.g. If there is a property as follows in yaml

  myObject:
       properties:
               myid:
                  type: int
                  x-app-extensions: {"aaid" : "bb", "ccid" : "dd"}
errors:
                   x-app-extensions: {"aaerr" : "bb", "ccerr" : "dd"}
    $ref: "#/definitions/errorModel"

Then, swagger model ignores vendorextensions for errors, and only has only $ref property in the parsed json node, and same in reflected in created java model. But, vendorextenion is correctly picked up for myid. The ignorance of vendor extension is due to an issue in following snippet of the code of io.swagger.util.PropertyDeserializer.propertyFromNode(JsonNode) method.

   JsonNode detailNode = node.get("$ref");
  if (detailNode != null) {
      return new RefProperty(detailNode.asText()).description(description);
  }

Is it expected that, $ref type nodes are not supposed to have any other properties?

wing328 commented 8 years ago

Similar to this one? https://github.com/swagger-api/swagger-codegen/issues/2172

sbelan commented 8 years ago

@wing328 this is not just limited to VendorExtenstions ... for a property of type $ref, non of the other attributes associated with that property are processed (like say, if I add minItems) The code should create RefProperty object, and should evaluate attributes associated with that property like any other normal property like say, StringProperty.

sbelan commented 8 years ago

@wing328 ... Please let me know, Is more input require on this?

wing328 commented 8 years ago

@sbelan given that both swagger parser and codegen have released a stable version, can you pull the latest master of swagger codegen to give it another try ?

sbelan commented 8 years ago

@wing328 ... I have planned migration activity from 2.1.5 to 2.1.6 for my application. I will let you know if issue still persist.

Nepoxx commented 8 years ago

I have the same issue on 2.1.6, however this extends to nested objects as well. Consider the following:

gs:
  x-name: gameState # does not work
  type: object
  properties:
    ud:
      x-name: userData # does not work
      title: userData
      type: object
      properties:
        n:
          x-name: name # this one works fine
          type: string

#elsewhere
responses:
      200:
        schema:
          type: object
          properties:
            gs:
              $ref: '#/definitions/gs' #as above
              x-name: gameState #also does not work

If I debug the models when generating, in the Model's JSON, vendorExtensions is empty. For the record I'm using the CSharpDotNet2 generator

wing328 commented 8 years ago

@Nepoxx please help pull the latest master to see if the issue has already been addressed.

Nepoxx commented 8 years ago

Issue is still present in 2.2.0

# ...
responses:
      200:
        description: A user's game state
        schema:
          type: object
          properties:
            gs:
              $ref: '#/definitions/gs'

gs:
  x-name: gameState # does not work
  type: object
  properties:
    v:
      x-name: version # works
      type: integer
    ud:
      x-name: userData # does not work
      title: userData
      type: object
      properties:
        n:
          x-name: name # this one works fine
          type: string

This is the debug output from generating:

{
  "importPath" : "IO.Swagger.Model.LoadGameState",
  "model" : {
    "name" : "LoadGameState",
    "classname" : "LoadGameState",
    "classVarName" : "LoadGameState",
    "modelJson" : "{\n  \"type\" : \"object\",\n  \"required\" : [ \"v\" ],\n  \"properties\" : {\n    \"v\" : {\n      \"type\" : \"integer\",\n      \"x-name\" : \"version\"\n    },\n    \"ud\" : {\n      \"$ref\" : \"#/definitions/LoadGameState_ud\"\n    }\n  },\n  \"x-name\" : \"gameState\"\n}",
    "classFilename" : "LoadGameState",
    "vars" : [ {
      "baseName" : "v",
      "getter" : "getV",
      "setter" : "setV",
      "datatype" : "long?",
      "datatypeWithEnum" : "long?",
      "name" : "V",
      "defaultValue" : "null",
      "defaultValueWithParam" : " = data.v;",
      "baseType" : "long?",
      "example" : "null",
      "jsonSchema" : "{\n  \"type\" : \"integer\",\n  \"x-name\" : \"version\"\n}",
      "hasMore" : true,
      "required" : true,
      "hasMoreNonReadOnly" : true,
      "isPrimitiveType" : true,
      "isNotContainer" : true,
      "isInteger" : true,
      "isEnum" : false,
      "vendorExtensions" : {
        "x-name" : "version" // <------------------------------------------- Good
      },
      "nameInCamelCase" : "V"
    }, {
      "baseName" : "ud",
      "complexType" : "LoadGameStateUd",
      "getter" : "getUd",
      "setter" : "setUd",
      "datatype" : "LoadGameStateUd",
      "datatypeWithEnum" : "LoadGameStateUd",
      "name" : "Ud",
      "defaultValue" : "null",
      "defaultValueWithParam" : " = data.ud;",
      "baseType" : "LoadGameStateUd",
      "example" : "null",
      "jsonSchema" : "{\n  \"$ref\" : \"#/definitions/LoadGameState_ud\"\n}",
      "isNotContainer" : true,
      "isEnum" : false,
      "vendorExtensions" : { }, <------------------------------------------------ Bad
      "nameInCamelCase" : "Ud"
    } ],
   "//" : "omitted the rest"
  }
}
achirita commented 7 years ago

@wing328 I have encountered the same issue while using the Swagger codegen. Is this considered a bug? I'm asking because there are a couple of similar issues where @webron explained that JSON References do not allow any additional properties and those would be ignored by JSON Reference parsers following the JSON Reference specification. This makes me think that it's not a bug and according to the JSON Reference specification, this is the way it is supposed to work. On the other hand, this issue is marked as a bug which is confusing for me. If this is considered a bug, has anyone found the root cause of this? Is it originating in the swagger-core module, PropertyDeserializer.propertyFromNode(...) method or somewhere in the swagger-parser module?

achirita commented 7 years ago

Seems like the problem can be solved by adding vendor extensions to RefProperty object in PropertyDeserializer.propertyFromNode(...). I would really appreciate if someone from the community can comment on this issue. @webron @wing328 :Is it an acceptable fix? If so I can contribute a pull request.

jakraska commented 6 years ago

This is also causing problems for me in v2.3.1 . Specifically with nested objects

  MainObj:
    type: object
    x-field-presence: true #good
    properties:
      propA:
        type: array
        x-field-presence: true #good
        items:
          type: object
          properties:
            propB:
              type: integer
              x-field-presence: true  #good            
            propC:
              type: object
              x-field-presence: true  #bad :(              
              properties:
                PropD:
                  type: string
                  x-field-presence: true #good