wcandillon / swagger-js-codegen

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

Store properties of a body parameter in method.bodySchema. #100

Open HawaiianSpork opened 8 years ago

HawaiianSpork commented 8 years ago

Adds the capability to access body properties from inside templates by adding a new method.bodySchema property.

paths:
    /pets/cat:
        post:
          parameters:
            - name: ''
              in: body
              schema:
                $ref: '#/definitions/Cat'
              required: true

definitions:
    Cat:
        type: object
        properties:
            name:
              description: 'Name of cat'
              type: string
            sex:
              description: 'Sex of cat, male or female'
              type: string
        required: ['sex']

produces the following following method.bodySchema that can be used in the template:

- name: name
  type: string
  description: 'Name of cat'
  required: false
- name: sex
  type: string
  description: 'Sex of cat, male or female'
  required: true

This is similar to pull request #94 but only adds bodySchema.

wcandillon commented 8 years ago

@HawaiianSpork Could you provide a template example that is using this feature?

HawaiianSpork commented 8 years ago

@wcandillon I updated the method.mustache template to include validation based on the new fields. Is that what you are looking for?

deinspanjer commented 7 years ago

The value for $ref is a path to the definitions section of the spec containing the definition of that parameter. For instance:

{
    "swagger": "2.0",
    "basePath": "/",
    "paths": {
        "/rpc/login": {
            "post": {
                "tags": [
                    "(rpc) login"
                ],
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "required": false,
                        "in": "header",
                        "name": "Prefer",
                        "type": "string",
                        "enum": [
                            "params=single-object"
                        ],
                        "description": "Preference"
                    },
                    {
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/(rpc) login"
                        },
                        "in": "body",
                        "name": "args"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    },
    "definitions": {
        "(rpc) login": {
            "required": [
                "_username",
                "_password"
            ],
            "properties": {
                "_username": {
                    "format": "text",
                    "type": "string"
                },
                "_password": {
                    "format": "text",
                    "type": "string"
                }
            },
            "type": "object"
        }
    }
}
deinspanjer commented 7 years ago

I pulled this request and merged with head and fixed the conflict. The merge was pretty much fine, just one small change to the line that has .contains() to change it to the new method name .includes(). I don't know if there is any way to generate a useful patch or anything to keep this PR up to date, but it was very useful to me and I'd like to see it merged.