[swagger-codegen] generated client in Python and PHP doesn't seem to handle Map[String, String]

662 views
Skip to first unread message

MLnick

unread,
Sep 20, 2013, 5:29:54 AM9/20/13
to swagger-sw...@googlegroups.com
Hi

I am using Scalatra for my API server, with docs generated by swagger-ui. I am then taking this spec from the API server and generating client code in Python and PHP.

My model looks something like this:

User
{"userId":X,"userData":{"field1":"val1","field2":"val2",...}}

It's a very simple representation. I want to have arbitrary data stored in a Map[String, String] in Scala.

Now, when I post a User using the Python code, all is well:

> user = User()
> user.userId = 1
> user.userData = {"field":"value"}
> uapi.postUser(user)

However, when I retrieve a user, I get:

In [15]: user = uapi.getUserById(1)

In [16]: user.userId
Out[16]: 1L

In [17]: user.userData
Out[17]: <models.User.User instance at 0x1052f5878>

In [18]: user.userData.userData

In [19]: 


So the Map[String, String] became a User instance, and the userData of that is blank.

Same seems to apply to PHP - I don't use PHP but a customer is telling me that the PHP client cannot deserialize the Map[String, String] piece

Any thoughts on how to fix this? It seems from the Python codegen that it cannot map "Map" to "dict" in Python. Not sure if there is a way around it other than to amend the generated code? 

Anyone else come across this?

Thanks
Nick

==== 
User.py looks like:

class User:
    """NOTE: This class is auto generated by the swagger code generator program.
    Do not edit the class manually."""


    def __init__(self):
        self.swaggerTypes = {
            'userData': 'Map[string,string]',
            'userId': 'long',
            'otherIds': 'list[String]'

        }


        #
        self.userData = None # Map[string,string]
        #
        self.userId = None # long
        #
        self.otherIds = None # list[String]

====

tony tam

unread,
Sep 20, 2013, 12:34:44 PM9/20/13
to swagger-sw...@googlegroups.com
Hi, Maps are currently not supported in the codegen.  They will be soon now that the spec is json-schema, but they currently don't work.

You could easily hack them in to python & php since they're dynamic languages.

Nick Pentreath

unread,
Nov 6, 2013, 3:48:57 AM11/6/13
to swagger-sw...@googlegroups.com
Hi Tony

I was just wondering how soon codegen would support Maps? 

Thanks
Nick


--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/XQA56sWWMb8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nick Pentreath

unread,
Dec 10, 2013, 1:39:01 AM12/10/13
to swagger-sw...@googlegroups.com
Hi,
Any news on this?

tony tam

unread,
Dec 10, 2013, 2:34:52 AM12/10/13
to swagger-sw...@googlegroups.com
Hi, still not implemented, but near the front of the list.
To unsubscribe from this group and all its topics, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

David Moles

unread,
Feb 28, 2014, 1:13:37 PM2/28/14
to swagger-sw...@googlegroups.com
I'm trying to hack something together in advance of formal support, and I'd like the transition to whatever you eventually come up with to be as smooth as I can make it. I'm sure I saw the proposed syntax somewhere, but I can't find it. Here's what I'm looking at now; is it anywhere close?

"myProperty": {"description": "my property","position": 0,"type": "map","items": {"$ref1": "integer","$ref2": "MyModelClass"}}

tony tam

unread,
Mar 2, 2014, 4:56:36 PM3/2/14
to swagger-sw...@googlegroups.com
Hi David,
This is a part of JSON Schema that is really lacking, at least per my understanding.  There are a couple different things.

* The `object` type is supposed to map to a dictionary structure (not to be confused with an object in the language-specific sense).  So a model like such:

class Person {
  private String name;
  private Map<String, Integer> childrensAges;
}

would look like this:

{
  "id": "Person",
  "properties": {
    "name": {
      "type": "string"
    },
    "childrensAges": {
      "type": "object",
      "additionalProperties": {
        "type": "integer",
        "format": "int32"
      }
    }
  }
}

So here, the Map<String, Integer> is described as an object, with "additionalProperties" defined to be an Integer.

I've also seen it suggested that maps be defined like this:

{
  "id": "Person",
  "properties": {
    "name": {
      "type": "string"
    },
    "childrensAges": {
      "type": "array",
      "items": [{
        "type": "string"
      },
      {
        "type": "integer",
        "format": "int32"
      }]
    }
  }
}

I don't really like either of these because the type "object" is really unintuitive, and the "items" in the second example is an array, when it typically is just a hash.  And worse, it's not uncommon to have a non-string key for maps, especially with scientific data.

That said, being true to the json-schema spec would imply that option #1 is the best way forward.

In your example, the "map" keyword isn't in the jsonschema lexicon for types, and would be rejected by most all parsers.  So I think we need to stay to the (limited) set of types:

number
integer
string
array
boolean
null
object

Make sense?

Nick Pentreath

unread,
May 26, 2014, 6:02:52 AM5/26/14
to swagger-sw...@googlegroups.com
Hi

Just wanted to check the status on this? 

If I'm using Scalatra, is the new version of swagger support able to handle Map[String, String], and if so how?


--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/XQA56sWWMb8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

Tony Tam

unread,
Jun 16, 2014, 7:30:31 PM6/16/14
to Nick Pentreath, swagger-sw...@googlegroups.com
Hi, the map is not supported in the spec, so I would not expect it to work in any generated clients automatically.
-- 
Tony Tam
Sent with Airmail
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages