swagger-api / swagger-api/swagger-codegen

additionalProperties=true is getting mis-categorized as UntypedProperty

Open
#7,586 0 comments 3 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

As per the definition of Free-Form Objects described in Swagger documentation here, additionalProperties: true needs to be treated as dictionary values can be of any type.

I assume any is interpreted as Object.class in the world of Java.
During the parsing or codegen modeling (not sure), such an object is treated as UntypedProperty causing following errors during codegen

[ERROR] No Type defined for Property io.swagger.models.properties.UntypedProperty@0
[ERROR] String to be sanitized is null. Default to ERROR_UNKNOWN

Actual: additionalProperties=true is treated as UntypedProperty. Generated class does not extend HashMap
Expected: additionalProperties=true needs to be treated as ObjectProperty. Generated class should extend HashMap

Sample notation:
pet.json << this one fails with above error

{
  "type": "object",
  "title": "pet",
  "additionalProperties": true,
  "properties": {
    "color": {
      "type": "string",
    }
  }
}

pet.json << this one works without any error

{
  "type": "object",
  "title": "pet",
  "additionalProperties": {
    "type": "object"
  },
  "properties": {
    "color": {
      "type": "string",
    }
  }
}
Swagger-codegen version

v2.3.1

Swagger declaration file content or url

swagger.json

{
  "swagger": "2.0",
  "paths": {
    "/pet": {
      "get": {
        "operationId": "petstoreGet",
        "responses": {
          "200": {
            "schema": {
              "$ref": "./pet.json"
            }
          }
        }
      }
    }
  }
}
Command line used for generation

Used maven plugin

<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
Steps to reproduce

Place above swagger.json and pet.json in same folder and run the maven plugin to generate java code.

Related issues/PRs
Suggest a fix/enhancement

Treat
"additionalProperties": true, exactly same as

"additionalProperties": {
  "type": "object"
}

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 by reproducing the issue with swagger.json and pet.json using the swagger-codegen Maven plugin version 2.3.1. Trace how additionalProperties=true is represented during parsing and codegen modeling, comparing it with the object-valued additionalProperties example. Done means generation completes without the reported errors and the generated Java class extends HashMap.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.