swagger-api / swagger-api/swagger-codegen

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

Open
#9,262 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

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:
https://github.com/swagger-api/swagger-codegen/issues/7586
https://github.com/swagger-api/swagger-core/issues/2507
https://github.com/swagger-api/swagger-codegen/issues/6896
https://github.com/swagger-api/swagger-core/issues/1437
https://github.com/swagger-api/swagger-codegen/issues/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.

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 io.swagger.codegen.SwaggerCodegen generate entry point and compare behavior between swagger-codegen 2.4.0 and 2.3.1, focusing on swagger-parser 1.0.35 versus 1.0.34. Reproduce the supplied inline Swagger schema with boolean additionalProperties; done means Java model classes are generated rather than silently omitted.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.