swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.9k stars 6.03k forks source link

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

Open hakamairi opened 2 years ago

hakamairi commented 2 years ago
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?

coditza commented 2 years ago

Same issue, trying to generate a jira cloud api python client, from https://developer.atlassian.com/cloud/jira/platform/swagger-v3.v3.json. I'm using the swaggerapi/swagger-codegen-cli-v3 docker image.

MaximValeev commented 9 months ago

i faced with the same problem for java serialization with jackson objectMapper. There is only additionalProperties in json but not actual ones. Is there any information about ht fix?

Nathanlclark commented 1 month ago

The two options I've found:

  1. Calling put() to add any properties in to the Map VehicleInfo vehicle = new VehicleInfo(); vehicle.put("info", new Info());
  2. Manually modify the Swagger Doc to set "additionalProperties": false

Has anyone found a better solution? I went with option 2 since it avoided a lot of trial and error.