Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wvell opened this issue Feb 17, 2019 · 6 comments
Open

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

wvell opened this issue Feb 17, 2019 · 6 comments

Comments

@wvell
Copy link

wvell commented Feb 17, 2019

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

@Martin0406
Copy link

Martin0406 commented Aug 29, 2019

Modify DartClientCodegen.java
languageSpecificPrimitives = new HashSet(
Arrays.asList(
"String",
"bool",
"int",
"num",
"double")
);

Add "Object" in "languageSpecificPrimitives"

ex:

languageSpecificPrimitives = new HashSet(
Arrays.asList(
"String",
"bool",
"int",
"num",
"double",
"Object")
);

@arvitaly
Copy link

Maybe it needs a pull request?

@kuhnroyal
Copy link

Having the same problem with OpenAPI-generator.

I don't think it should be a primitive type. A Spec Object should become a Map<String, dynamic> in Dart and not an Object.

With the OpenAPI-generator you can workaround it by providing the template option to the generator and overriding the class.mustache template.

@maxfriedmann
Copy link

@kuhnroyal Can you provide a tiny how-to for people like me that aren't familiar with the OpenAPI Generator? We're currently doing a regex after the code generation 🤔

@kuhnroyal
Copy link

@maxfriedmann Basically you copy the class.mustache file from the repository into a folder foo and make the mentioned change. Then you pass -t foo to the generator.

You can find the templates here: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources/dart (use the correct tag/branch) - your folder foo is just an overlay for this, anything that is found in your folder will be used instead of the default.

@maxfriedmann
Copy link

maxfriedmann commented Sep 15, 2020

@kuhnroyal cool, that sounds easy enough to even me! Thanks a lot

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

No branches or pull requests

5 participants