Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Bogus Models generated in schema #595

Closed
paultiplady opened this issue Dec 8, 2016 · 4 comments
Closed

Bogus Models generated in schema #595

paultiplady opened this issue Dec 8, 2016 · 4 comments

Comments

@paultiplady
Copy link

I'm trying to use swagger-codegen to generate a Python client from my django-rest-swagger schema (django-rest-swagger==2.0.7, djangorestframework==3.5.3).

I am getting a lot of models named Data*, and no client models related to my actual API models.

For an example of an endpoint that's being rendered incorrectly, the following CoreAPI endpoint:

       "update": {
            "_type": "link",
            "url": "/accounts/{id}/",
            "action": "put",
            "encoding": "application/json",
            "description": "The accounts for all Users.",
            "fields": [
                {
                    "name": "id",
                    "required": true,
                    "location": "path"
                },
                {
                    "name": "account_number",
                    "required": true,
                    "location": "form",
                    "type": "string"
                },
                ...
            ]
        }

Renders to this path in the swagger.json:

  "paths" : {
    "/accounts/{id}/" : {
      "put" : {
        "tags" : [ "accounts" ],
        "summary" : "The accounts for all Users.",
        "description" : "The accounts for all Users.",
        "operationId" : "accounts_update",
        "consumes" : [ "application/json" ],
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "description" : "",
          "required" : true,
          "type" : "string"
        }, {
          "in" : "body",
          "name" : "data",
          "required" : false,
          "schema" : {
            "type" : "object",
            "required" : [ "account_number", "name", "routing_number" ],
            "properties" : {
              "account_number" : {
                "type" : "string",
                "description" : ""
              },
              ...
            }
          }
        } ],
        "responses" : {
          "200" : {
            "description" : ""
          }
        }
      },
      ...

Note the "in": "body", "name": "data" in the generated swagger JSON.

The output model has the correct fields, but incorrect name:

class Data1(object):
    """
    NOTE: This class is auto generated by the swagger code generator program.
    Do not edit the class manually.
    """
    def __init__(self, account_number=None, ...):
        """
        Data1 - a model defined in Swagger

        :param dict swaggerTypes: The key is attribute name
                                  and the value is attribute type.
        :param dict attributeMap: The key is attribute name
                                  and the value is json key in definition.
        """
        self.swagger_types = {
            'account_number': 'str',
            ...
        }
        ...

Any suggestions for what might be going wrong, or requests for further diags to collect?

@paultiplady
Copy link
Author

Is swagger-codegen expected to work with the schemas that django-rest-swagger 2.* produces? I'd hate to be wasting my time here, and I've seen some passing comments suggesting that response schemas are required for the codegen client to correctly build models. I've not dug in to verify whether that's true though.

@kevin-brown
Copy link

If I were to guess, I'd say that the following part of the generated schema...

...
          "in" : "body",
          "name" : "data",
          "required" : false,
          "schema" : {
...

...is probably the reason for generating a bunch of "data" models.

Now, I have no idea whether or not CoreAPI is generating the correct Swagger schema, or if this issue has already been fixed. I'm not familiar enough with either specifications to be able to make that judgement call.

@marcgibbons
Copy link
Owner

@paultiplady This field is set by the openapi-codec and, I don't think there is any particular reason why this field couldn't be named after the serializer. See https://github.com/core-api/python-openapi-codec/blob/master/openapi_codec/encode.py#L167.

Feel free to open an issue on the codec repo / propose a pull request there and reference this issue.

Thanks.

@paultiplady
Copy link
Author

Thanks for digging in -- hadn't realized that much was generated in the CoreAPI code. I'll chase this down on the CoreAPI side.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants