Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bzb0 opened this issue Nov 5, 2018 · 5 comments

Comments

@bzb0
Copy link

bzb0 commented Nov 5, 2018

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: #1262
Swagger Code Generator: swagger-api/swagger-codegen#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

@wing328
Copy link
Member

wing328 commented Nov 5, 2018

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)

@bzb0
Copy link
Author

bzb0 commented Nov 6, 2018

@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")

@codemoped
Copy link

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).

@vandeseer
Copy link

If I am not mistaken, this is the same as: #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!

@FatCash
Copy link

FatCash commented Sep 3, 2019

+1
I think its essentially the same issue @vandeseer.
My suggestion from user perspective (don't know if its possible in code)
#1466 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants