openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[JAVA] Object type with 'additionalProperties' not annotated/generated properly

Open bzb0 opened this issue 8 years ago • 5 comments

Description

We have the following type/object, namely it is a dictionary/map, but it has one custom property, namely 'extras'.

links:
          description: Links object
          type: object
          additionalProperties:
             $ref: "#/components/schemas/hrefType"
          properties:
             extras:
                $ref: "#/components/schemas/hrefType"
 components:
       schemas:
              hrefType:
                    description: Link to a resource
                    type: string
                    example: "/test/1234"

The following code will be generated. If we set the property extras via the builder method, it will not be part of the serialized JSON object.

public class Links extends HashMap<String, String>  {
  @JsonProperty("extras")
  private String extras;

  public Links extras(String extras) {
    this.extras = extras;
    return this;
  }
  // ...
}
openapi-generator version

3.3.1

OpenAPI declaration file content or url

See description.

Command line used for generation

/

Steps to reproduce

/

Related issues/PRs

OpenAPI Generator: https://github.com/OpenAPITools/openapi-generator/pull/1262 Swagger Code Generator: https://github.com/swagger-api/swagger-codegen/pull/8245

Suggest a fix/enhancement

The generated code, should look like as proposed in the following Stackoverflow answers: https://stackoverflow.com/a/31321861 https://stackoverflow.com/a/31321821 https://stackoverflow.com/a/44628460

bzb0 avatar Nov 05 '18 15:11 bzb0

What does the payload look like? Maybe we can model it differently.

According to the SO link you provided:

It's generally considered bad practice to extend HashMap. Instead, you should create a class that has a Map, and the additional field.

I tend to agree (or I miss the benefit of extending HashMap)

wing328 avatar Nov 05 '18 15:11 wing328

@wing328 what do you mean, how does the payload look like? If we simply set the property extras and don't add an extra property via object.put(key, value), the serialized JSON object is empty. Here an example:

Links links = new Links().extras("https://www.example.com"); will result in {}.

In order to set the property extras we have to set it explicitly like this:

Links links = new Links();
links.put("extras", "https://www.example.com")

bzb0 avatar Nov 06 '18 08:11 bzb0

I currently have the same issue (generator version 3.3.2). Having a fixed key in a dictionary as shown in the OpenAPI docs:

type: object
properties:
  mykey:
    type: string
required:
  - mykey
additionalProperties:
  type: string

The generated model includes the following block:

@JsonProperty("mykey")
private String mykey;

public MyClass mykey(String mykey) {
  this.mykey = mykey;
  return this;
}

@ApiModelProperty(required = true, value = "")
@NotNull
public String getMykey() {
  return mykey;
}

public void setMykey(String mykey) {
  this.mykey = mykey;
}

The property 'mykey' is not set when deserializing (Jackson 2.9.7). I'm guessing that a map entry is created instead. But since the property is required and annotated with NotNull, the validation fails (javax.validation 2.0.1.Final).

codemoped avatar Nov 06 '18 16:11 codemoped

If I am not mistaken, this is the same as: https://github.com/OpenAPITools/openapi-generator/issues/1466

Will there be any fix? Can it be fixed by simply changing the template (that would provide us with an easy workaround) or is there a deeper change in the generator code necessary?

Any update would be very appreciated!

Thanks!

vandeseer avatar Jul 07 '19 15:07 vandeseer

+1 I think its essentially the same issue @vandeseer. My suggestion from user perspective (don't know if its possible in code) https://github.com/OpenAPITools/openapi-generator/issues/1466#issuecomment-527419227

FatCash avatar Sep 03 '19 11:09 FatCash