swagger-api / swagger-api/swagger-codegen

[Swift4] Post requests with a body parameter as ModelObject are malformed.

Open
#10,326 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

In swift4 code generation, when custom templates introduced via -t command, the generated POST which require a modelObject as a parameter are incorrectly constructed.

Normally, the request function should only ask for the modelObject, however it asks for both the modelObject and the parameters inside the modelObject.

Example request and modelObject images are added below.

Example malformed post request.
Screen Shot 2020-06-18 at 18 04 29

Example post request body object.
Screen Shot 2020-06-18 at 18 03 16

Extra Clarification: Just to make it crystal clear, the request function should not ask for profileId & answers parameters in the above case since the body object already has the same parameters inside.

I'm using the 3.0.20 version swagger-codegen-cli.jar's mustache templates with some custom changes in them.
I thought that I may have broken something, so i tried to use the original files but the result is same as long as i introduce the templates with -t command.

Note that, If I do not specify templates then it functions correctly.

Swagger-codegen version

3.0.20

JSON & Config file

Below you can request specific json, which includes the request and the parameter modelObject definition

    "paths": {
        "/v1.3/pro/job_preferences": {
            "post": {
                "responses": {
                    "201": {
                        "description": "Created",
                        "schema": {
                            "$ref": "#/definitions/Void"
                        }
                    },
                    "404": {
                        "description": "NotFound",
                        "schema": {
                            "$ref": "#/definitions/ErrorMessage"
                        }
                    },
                    "500": {
                        "description": "InternalServerError",
                        "schema": {
                            "$ref": "#/definitions/ErrorMessage"
                        }
                    },
                    "401": {
                        "description": "Unauthorized",
                        "schema": {
                            "$ref": "#/definitions/ErrorMessage"
                        }
                    },
                    "400": {
                        "description": "BadRequest",
                        "schema": {
                            "$ref": "#/definitions/ErrorMessage"
                        }
                    }
                },
                "parameters": [
                    {
                        "schema": {
                            "$ref": "#/definitions/SaveProJobPreferencesViewModel"
                        },
                        "required": true,
                        "name": "jobPreferencesViewModel",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json",
                    "text/json",
                    "text/html"
                ],
                "deprecated": false,
                "tags": [
                    "Pro"
                ],
                "consumes": [
                    "application/json",
                    "text/json",
                    "text/html",
                    "application/x-www-form-urlencoded"
                ],
                "operationId": "Pro_SaveProJobPreferences"
            }
        }
    },
    "definitions": {
        "SaveProJobPreferencesViewModel": {
            "type": "object",
            "properties": {
                "profile_id": {
                    "type": "integer",
                    "format": "int32"
                },
                "answers": {
                    "items": {
                        "$ref": "#/definitions/ProJobPreferenceAnswerViewModel"
                    },
                    "type": "array"
                }
            }
        }
    }
}
Command line used for generation

swagger-codegen generate -i api.json -l swift4 -c config.json -t MainApiMustaches

config.json

{
      "responseAs":"PromiseKit"
}

MainApiMustaches are the mustache files of version 3.0.20 swagger-codegen-cli.jar with some custom change in them.

I couldn't understand why the generator acts this way when even if the introduced templates are exactly the same templates extracted from the .jar the generation result changes with -t command.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Swift4 generation command, the extracted MainApiMustaches templates, and the supplied Swagger JSON/configuration. Compare generation with and without -t, focusing on the POST signature for jobPreferencesViewModel. Done means custom templates produce a request that accepts only the body model object, matching the non-template generation.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.