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

[BUG][resttemplate] Object with additionalProperties (HashMap) does not generate a model #3602

Closed
Na3blis opened this issue Aug 9, 2019 · 8 comments · Fixed by #4569
Closed

Comments

@Na3blis
Copy link

Na3blis commented Aug 9, 2019

Description

I am trying to generate a HashMap class, but no class is generated and a plain Java Map is used where the class ContextHeaders should be used.

  @JsonProperty("contextHeaders")

  private Map contextHeaders = null;
openapi-generator version

4.0.3

OpenAPI declaration file content or url

swagger.json
swagger-client-config.json

Command line used for generation

curl -s -o ./codegen.jar http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.0.3/openapi-generator-cli-4.0.3.jar
java -jar ./codegen.jar generate -c ./swagger-client-config.json -i ./swagger.json -g java -o generated-client/

Steps to reproduce

Run the codegen 4.0.3 against the swagger.json file with the config swagger-client-config.json. Notice there is no com.example.authrules.v1.client.model.ContextHeaders class, and com.example.authrules.v1.client.model.RulesUser references a plain Java Map instead of a class

@auto-labeler
Copy link

auto-labeler bot commented Aug 9, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@jmini
Copy link
Member

jmini commented Aug 13, 2019

Can you please switch to a newer version of OpenAPI generator?

Can you provide a small OpenAPI-Spec that show what is not working for you?

@Na3blis
Copy link
Author

Na3blis commented Aug 13, 2019

@jmini I just tried with 4.1.0 same results, as well as with the 4.2 snapshot.

Here is what I'm referring to. Currently I'm using a 2.0 spec, and swagger-codegen 2.3.1 and it generates ContextHeaders.java.
And in the model that references that, it has:

  @JsonProperty("contextHeaders")
  private ContextHeaders contextHeaders = null;

However, when using the 3.0 spec, it still creates a HashMap<String, List>, the problem is that it's not it's own Model Object as before, where it's referenced it shows up now as

  public static final String JSON_PROPERTY_CONTEXT_HEADERS = "contextHeaders";
  @JsonProperty(JSON_PROPERTY_CONTEXT_HEADERS)
  private Map<String, List<String>> contextHeaders = new HashMap<String, List<String>>();

@jmini
Copy link
Member

jmini commented Aug 14, 2019

I think that with generateAliasAsModel you get the Swagger-Codegen behavior where models (ContextHeaders extends Map<String, List<String>>) are created .

See: #472

@Na3blis
Copy link
Author

Na3blis commented Aug 14, 2019

Thanks, that does look like the solution! However it does not create the object correctly when referenced,
[ERROR] /Users/tim/dev/authorization-rules-service/generated-client/src/main/java/com/example/authrules/v1/client/model/RulesUser.java:[146,43] incompatible types: java.util.HashMap<java.lang.String,java.util.List<java.lang.String>> cannot be converted to com.example.authrules.v1.client.model.ContextHeaders

And in the RulesUser class it has:

public static final String JSON_PROPERTY_CONTEXT_HEADERS = "contextHeaders";
  @JsonProperty(JSON_PROPERTY_CONTEXT_HEADERS)
  private ContextHeaders contextHeaders = new HashMap<String, List<String>>();

So it's trying to instantiate it as a HashMap instead of as a new ContextHeaders object.

@bodograumann
Copy link
Contributor

We have just encountered the same behaviour as @Na3blis. The problem is that, while DefaultGenerator does generate the model definition file in the generateModels method, DefaultCodegen does not reference them in the fromRequestBody method.

I'll try to fix it.

@Praveen2518
Copy link

@Na3blis did you get the answer why ContextHeaders object created with HashMap and how can we fix this

@Na3blis
Copy link
Author

Na3blis commented Sep 20, 2021

@Na3blis did you get the answer why ContextHeaders object created with HashMap and how can we fix this

Unfortunately not. I think I ended up having to change the api to work around this

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

Successfully merging a pull request may close this issue.

4 participants