Skip to content

[RUBY] client, NameError when parsing nested anonymous objects #3880

@cgardens

Description

@cgardens
Description

The ruby client does not seem able to correctly parse nested anonymous objects.

ex. a route that returns this data structure

name_by_child_id_by_parent_id: {
  <parent_id>: { 
    <child_id>: "John",
    <child_id>: "Sandra",
  },
  <parent_id>: { 
    <child_id>: "Caroline",
    <child_id>: "Lester",
  },
}, ...

Fails in the client with this error:

NameError (wrong constant name String, Hash<String)

This appears to be due to the fact that this regex, is unable to correctly parse the data structure type.

The data structure is:

Hash<String, Hash<String, String>>

This is how it was parsed by the client:

# key
String, Hash<String
# value
String>

This is how it should have been parsed:

# key
String
# value
Hash<String, String>

The problem code is here:
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache#L49

The following change fixes this problem:

# broken regex
/\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
# correct regex
/\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/

Will submit a corresponding PR.

My suggested change would break this somewhat silly edge case:

Hash<Hash<String, String>, String>
# would be parsed as:
# key 
Hash<String
# value
String>, String

But it seems like the former case makes far more sense to support than the latter. Just pointing this out for due diligence.

Note: This is a somewhat contrived example to illustrate the problem. This data structure is more warranted in the "real" use case.

Swagger-codegen version

2.2.0 (installed via brew)

Swagger declaration file content or url

For reference here's the yaml I'm using to generate this response.

definitions:  
  FamiliesObject:
    type: object
    properties:
      name_by_child_id_by_parent_id:
        type: object
        additionalProperties:
          type: object
          additionalProperties:
            type: string
Command line used for generation

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
Rails 4.2.5

swagger-codegen generate -i families.yml -l ruby -o /tmp/test/ -c families_app_config.json

Steps to reproduce
  1. create an object (ex. the FamiliesObject above).
  2. add it to your responses schema for a route that returns nested anonymous hashes.
  3. run codegen
  4. hit route from an application using the generated client.
Related issues

I went through open and closed issues, and couldn't find anything directly related.

Suggest a Fix

pr: #3879

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions