swagger-api / swagger-api/swagger-codegen

[JAVA] additionalProperties: true makes the deserialization ignore the actual properties

Open
#11,619 3 comments 1 reaction 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

So I'm generating a client for yaml similar to the one below.
They have types in schema that have their own properties and are open for new ones (additionalProperties: true).
The expectation would be that when I call the service I get a typed response and all or remaining other properties in the map.
Unfortunately I get all the values in key -> value map and none in the typed properties.

Seems to work not the way I would expect.

Swagger-codegen version

3.0.30

Swagger declaration file content or url
openapi: 3.0.1
info:
  title: example-info
  version: 1.0.0
  contact:
    name: test
    email: test@me.nospam
servers:
  - url: http://localhost:8080/example
paths:
  /poc:
    get:
      operationId: getPoc
      responses:
        '200':
          $ref: '#/components/responses/success_poc'
        '400':
          description: Bad Param
          content: {}
        '500':
          description: Internal Server Error
          content: {}
components:
  responses:
    success_poc:
      description: OK
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/vehicleInfo'
  schemas:
    info:
      title: info
      type: object
      required:
        - prop1
        - prop2
      additionalProperties: true
      properties:
        prop1:
          type: boolean
        prop2:
          type: boolean
    vehicleInfo:
      type: object
      required:
        - info
      additionalProperties: true
      properties:
        info:
          $ref: '#/components/schemas/info'
    
Command line used for generation
java -jar swagger-codegen-cli-3.0.30.jar generate -l java -i poc.yaml -o poc-client-dir -c poc-config.json

The config json:

{
"hideGenerationTimestamp": true,
"dateLibrary": "java8",
"useRuntimeException": true,
"artifactId": "poc",
"artifactVersion": "1.0.0"
}

And the generated VehicleInfo model:

@Schema(description = "Vehicle info response")

public class VehicleInfo extends HashMap<String, Object> {
  @SerializedName("info")
  private Info info= null;

  public VehicleInfo info(Info info) {
    this.info= info;
    return this;
  }

   /**
   * Get info
   * @return info
  **/
  @Schema(required = true, description = "")
  public Info getInfo() {
    return info;
  }

  public void setInfo(Info info) {
    this.info= info;
  }

I've tried this using JAVA and default gson as well as jersey2.

Steps to reproduce

Generate a client from yaml, try to use it and expect from the response object the type property to be set.
Instead everything is treated as additional value and stored in map.

Related issues/PRs
Suggest a fix/enhancement

Would it be possible to populate the existing properties?

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

Use the supplied poc.yaml and generation command to reproduce the behavior with Java and the default Gson or Jersey2 setup. Inspect the generated VehicleInfo model and the generator handling for schemas with additionalProperties: true. Done means typed properties such as info are populated during deserialization while unknown properties remain available in the map.

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.