swagger-api / swagger-api/swagger-codegen

[dart] "type: object" with no properties generates invalid code

Open
#9,182 6 comments 2 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

Generating a swagger api that has definitions of type=object and no properties generates Object.mapFromJson(json['parameterBag']). This code is not valid and it should(I think) generate json['parameterBag'].

The api I am using is returning a map with key value pairs that are dynamic. That is why there are no properties defined in the swagger file.

Swagger-codegen version

2.4.1

Swagger declaration file content or url
{
    "host": "localhost",
    "basePath": "/",
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "schemes": [
        "http"
    ],
    "swagger": "2.0",
    "definitions": {
        "SendRequest": {
            "properties": {
                "parameterBag": {
                    "additionalProperties": {
                        "type": "object"
                    },
                    "type": "object"
                }
            },
            "type": "object"
        },
        "SendResponse": {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "properties": {
                "msg": {
                    "type": "string"
                }
            },
            "type": "object"
        }
    },
    "paths": {
        "/send": {
            "post": {
                "description": "",
                "operationId": "send",
                "parameters": [
                    {
                        "description": "",
                        "in": "body",
                        "name": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/SendRequest"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Success",
                        "schema": {
                            "$ref": "#/definitions/SendResponse"
                        }
                    },
                    "400": {
                        "description": "Validation incorrect"
                    },
                    "404": {
                        "description": "Not found"
                    }
                }
            }
        }
    }
}
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i dart.json -l dart -o generated_code/ -D browserClient=false

Steps to reproduce
  • Use type object with no properties
  • Compile the code
Suggest a fix/enhancement

If the type has no properties do not render the Object.mapFromJson

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

Run the supplied swagger-codegen-cli command with the declaration in the issue and inspect the Dart generator templates that produce parameterBag deserialization. Verify the generated client compiles and that an empty object schema no longer emits the invalid Object.mapFromJson call.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.