wcandillon / swagger-js-codegen

A Swagger Codegen for typescript, nodejs & angularjs
Apache License 2.0
693 stars 286 forks source link

Swagger body parameters #91

Open mm-gmbd opened 8 years ago

mm-gmbd commented 8 years ago

I'm not sure if this is me misunderstanding the Swagger specification, or if this is a bug, but I'll go ahead and lay it out.

According to the specification, a Path Item Object can contain a field named parameters, which is an array of either Parameter Objects or Reference Objects. Let's focus on the parameter objects.

The parameter object contains a number of fields, including the required field in. This field can be one of the following possible values: "query", "header", "path", "formData" or "body".

In my case, I have a Swagger, in one of my Path Item Objects, I have a singular parameter object in my parameters field. This singular parameter object's in field is set to body. According to the specification, if in is set to body, another parameter must be present, schema, which is of type Schema Object, which is based on the JSON Schema Specification Draft 4.

In this schema object, I can define one to many properties that should essentially be considered body parameters. But, my issue is that the template variable parameters does not contain the variables that are defined as body parameters, it only contains one parameter named body.

Here is my swagger:

{
    "swagger": "2.0",
    "info": {
        "title": "",
        "version": "",
        "description": ""
    },
    "paths": {
        "/some/resource": {
            "post": {
                "responses": {
                    "200": {
                        "description": "OK",
                        "headers": {},
                        "examples": {
                            "application/json": {
                                "prop1": "a string"
                            }
                        },
                        "schema": {
                            "type": "object",
                            "properties": {
                                "prop1": {
                                    "type": "string",
                                    "required": true
                                }
                            }
                        }
                    }
                },
                "summary": "Some resource",
                "description": "",
                "tags": [
                    "(no tags)"
                ],
                "parameters": [
                    {
                        "name": "body",
                        "in": "body",
                        "schema": {
                            "type": "object",
                            "properties": {
                                "prop1": {
                                    "type": "string",
                                    "required": true
                                }
                            }
                        }
                    }
                ]
            }
        }
      }
}

And here is the resulting Node.js code, specifically the portion that uses the method isBodyParameter:

if (parameters['body'] !== undefined) {
        body = parameters['body'];
    }

There is no mention of the body parameter param1. It would be very helpful if, when one of the Swagger parameters is in body and there is a schema defined, those body parameters would be accessible via the template variables.

Ideas / comments?

Thanks, -Max

wcandillon commented 8 years ago

@mm-gmbd which are the body parameters you are referring to? In the swagger sample above I see only one named body. Given the swagger sample above, could you send me an example of expected generated source code? It would help to understand the issue a bit better.

mm-gmbd commented 8 years ago

@wcandillon, sorry for the late reply. Correct, there is only one parameter named body, and that is actually enforced by the swagger spec itself:

Since there can only be one payload, there can only be one body parameter. The name of the body parameter has no effect on the parameter itself and is used for documentation purposes only.

Also:

If in is "body":

Field Name Type Description
schema Schema Object Required. The schema defining the type used for the body parameter.

The schema object that must be included if the parameter is a body parameter defines the actual schema of the body. Therefore, whatever is in the schema object are actually the body parameters that should be cared about. In the example above, this would be prop1, and this is not currently accessible via the mustache variables.

I've forked this and can provide a PR with my changes. They are somewhat quick and dirty, but it may help provide some insight into what I'm after.