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

[Java] additionalProperties=false is categorised as UntypedProperty causing code generation to silently fail #9262

Open
matt-s-wise opened this issue Mar 13, 2019 · 1 comment

Comments

@matt-s-wise
Copy link

matt-s-wise commented Mar 13, 2019

Description

With additionalProperties: false (or indeed true) in schema structure in response object, code generation silently fails. This is with inline schema object, it appears to work when using refs.

The reason is that a boolean additionalProperties resolves to UntypedProperty in the Swagger parsing.

Documentation on the Internet is rather ambiguous as to what the valid types for additionalProperties are, but from what I gather, it is either boolean or object. By default, Swagger is supposed to treat a missing additionalProperties as additionalProperties: false, so explicitly specifying this should be benign and should certainly not break code generation entirely.

Looks like the official Swagger spec definition is here (have linked to the specific line re additionalProperties): https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v2.0/schema.json#L1004

Swagger-codegen version

This is a regression bug introduced in codegen v2.4.0. It works as expected in codegen v2.3.1.
More specifically, swagger-parser v1.0.35 breaks the code generation; I can override this dependency with v1.0.34 to get code generation to work but this is hacky, swagger-codegen should resolve its own dependencies with no overriding.

Swagger declaration file content or url
{
  "swagger": "2.0",
  "info": {
    "version": "v2.2",
    "title": "Dummy specification"
  },
  "paths": {
    "/foo": {
      "get": {
        "responses": {
          "200": {
            "description": "Successful response",
            "schema": {
              "type": "object",
              "properties": {
                "MyProp1": {
                      "type": "string"
                },
                "MyProp2": {
                      "type": "integer"
                }
              },
              "additionalProperties": false
            }
          }
        }
      }
    }
  }
}
Command line used for generation

io.swagger.codegen.SwaggerCodegen generate -i test.swagger.json -o generated -l java

Steps to reproduce

Run the above command. Note that no Java classes in the default model output io.swagger.client.model package are generated in codegen v2.4.0, but are generated if the same is run with codegen v2.3.1.

Pasting in to editor.swagger.io/# and generating Swagger Java Client also exhibits the problem of non-generation of the actual model classes.

Related issues/PRs

Similar issues:
#7586
swagger-api/swagger-core#2507
#6896
swagger-api/swagger-core#1437
#1318

Suggest a fix/enhancement

A boolean additionalProperties must not be treated as UntypedProperty in the parsing, this seems to cause the knock on effects that cause code generation to silently fail.

@huchevsky
Copy link

I found a workaround for this issue. You should define response schema in 'definitions' and then just reference it. Here is an example:
{ "swagger": "2.0", "info": { "version": "v2.2", "title": "Dummy specification" }, "definitions": { "ResponseObj": { "type": "object", "properties": { "MyProp1": { "type": "string" }, "MyProp2": { "type": "integer" } }, "additionalProperties": false } }, "paths": { "/foo": { "get": { "responses": { "200": { "description": "Successful response", "schema": { "$ref": "#/definitions/ResponseObj" } } } } } } }

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

2 participants