swagger-api / swagger-api/swagger-codegen

[RUBY] Deserialization should honor discriminator field when deserializing collection

Open
#8,247 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Let's say I have collection that contains LogicalRouterPort in API spec:

"LogicalRouterPortListResult": {
      "allOf": [
        {
          "required": [
            "results"
          ],
          "type": "object",
          "properties": {
            "results": {
              "items": {
                "$ref": "#/definitions/LogicalRouterPort"
              },
              "type": "array",
              "description": "Logical router port list results",
              "title": "Logical router port list results"
            }          }        }      ]

LogicalRouterPort has with resource_type as discriminator.

"LogicalRouterPort": {
      "allOf": [   
        {
          "discriminator": "resource_type",
          "required": [
            "resource_type"
          ],
          "type": "object",
          "properties": {
            "resource_type": {
              "enum": [
                "LogicalRouterLinkPortOnTIER0",
                "LogicalRouterLinkPortOnTIER1",
              ],
              "type": "string",
            }, }   }  ] }

and here is a inherited class of LogicalRouterPort

"LogicalRouterLinkPortOnTIER0": {
      "allOf": [ {
          "properties": {
            "linked_logical_router_port_id": {
              "readOnly": true,
              "type": "string",
            }
          },
          "type": "object" }   ],  }

I have JSON that represents LogicalRouterPortListResult and actual values in the results field are objects of class LogicalRouterLinkPortOnTIER0 with resource_type set to LogicalRouterLinkPortOnTIER0.
So when I'm deserializing LogicalRouterPortListResult I would expect ruby object to have proper ruby type in results field. Instead my LogicalRouterPortListResult has results of type LogicalRouterPort.
This is happening because my ruby class LogicalRouterPortListResult has hardcoded type of LogicalRouterPort and it uses this type (instead of getting actual type from resource_type) to deserialize JSON.

    def self.swagger_types {
          <reducted>
          :'results' => :'Array<LogicalRouterPort>'
      }
    end
Swagger-codegen version

2.3.1

Swagger declaration file content or url

This is description of whole project that includes objects from above
api spec

Command line used for generation

swagger-codegen generate -i ./data/nsx_api.json -l ruby -o ./lib/nsxt -c ./data/config.json -t ./data/swagger-nsxt-template

Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

As a workaround I've added

unless value[:resource_type ].nil?
    type = value[:resource_type].to_sym
end
temp_model = {{moduleName}}.const_get(type).new
temp_model.build_from_hash(value)

in deserialization code here

Am I missing something? Is it really a bug do I just have incorrect spec file?

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 with the Ruby deserialization logic in modules/swagger-codegen/src/main/resources/ruby/base_object.mustache, then generate the client with the provided command and nsx_api.json. Compare deserialization of LogicalRouterPortListResult.results when resource_type identifies LogicalRouterLinkPortOnTIER0. Done means generated Ruby objects in the collection honor the discriminator instead of always using LogicalRouterPort.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.