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] [JAVA] "additionalProperties" does not generate a HashMap Model, openapi: 3.0.2 #7066

Open
roostapour opened this issue Jul 28, 2020 · 25 comments

Comments

@roostapour
Copy link

Description

I am using "additionalProperties" in my yaml file as below.

openapi: 3.0.2
....
components:
  schemas:
    Messages:
      type: object
      additionalProperties:
        $ref: '#/components/schemas/Message'
    Message:
      type: object
      properties:
        code:
          type: integer
        text:
          type: string

But when running mvn clean package, no class is being created for Messages. I was expecting to see a class like :

public class Messages extends HashMap<String, Object> {
......
}

in org.openapitools.client.model.Messages the same as other classes generated based on the yaml file. I tried additionalProperties: true and additionalProperties: {} but none of them worked. It seems like a bug

my pom.xml for openapi-generator and swagger versions
<dependency>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-annotations</artifactId>
  <version>1.6.1</version>
</dependency>
....
<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.3.1</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
        ...
    </execution>
  </executions>
</plugin>
Command line used for generation

mvn clean package

@roostapour roostapour changed the title [BUG] [JAVA} "additionalProperties" does not generate a HashMap Model, openapi: 3.0.2 [BUG] [JAVA] "additionalProperties" does not generate a HashMap Model, openapi: 3.0.2 Jul 28, 2020
@wing328
Copy link
Member

wing328 commented Jul 29, 2020

Please try the option generateAliasAsModel, which will generate array, map as models

@roostapour
Copy link
Author

Thanks for your answer @wing328 . I tried both

<configuration>
    <generateAliasAsModel>true</generateAliasAsModel>
</configuration>

and

<configuration>
    <configOptions>
         <generateAliasAsModel>true</generateAliasAsModel> 
    </configOptions>
</configuration>

But none of them worked. How should I set the generateAliasAsModel option?

@wing328
Copy link
Member

wing328 commented Jul 29, 2020

@roostapour
Copy link
Author

@wing328 I realized if I define a Map of string to string in the yaml file like the following then generateAliasAsModel works.

Messages:
  type: object
  additionalProperties:
    type: string

But if the value is an object or ref (which I need) like the the following then generateAliasAsModel does not work and doesn't generate Messages

Messages:
  type: object
  additionalProperties:
    type: object

or

Messages:
  type: object
  additionalProperties:
    $ref: '#/components/schemas/Message'

@roostapour
Copy link
Author

Just to add, I do have the same issue on javascript side. Having the following version in my package.json

"devDependencies": {
    "@openapitools/openapi-generator-cli": "^1.0.13-4.3.1"
}

and running with --generate-alias-as-model flag, with the same yaml file behaves exactly the same as Java side. For Map of string to string it works but for Map of string to object/ref it does not generate the model.

@abhijith-prabhakar
Copy link

abhijith-prabhakar commented Jul 30, 2020

It is slightly different issue in JAX-RS. I am working on v4.3.1 and OpenAPI v2

It generates Message.java but the pojo where messages are included does not have type information. This generates a compile error.

private Message messages = new HashMap<>();

I traced it to below code on pojo.mustache

private {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};

I am not sure how HashMap<> is getting substituted here. Please let me know if you want me to create a separate issue for this

@roostapour
Copy link
Author

Can someone please answer this?

@wing328
Copy link
Member

wing328 commented Aug 7, 2020

@roostapour based on what you provided, looks like it's a bug. A good starting point is https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java and search for isGenerateAliasAsModel

@yahyavi
Copy link

yahyavi commented Aug 14, 2020

Hello,
We have a similar issue trying to generate a Map from our YAML file (v3). Is there a document/example on how to generate maps/dictionaries properly?

@chrylis
Copy link

chrylis commented Aug 18, 2020

Confirmed that as of 4.3.1 classes are generated, but:

  • The classes incorrectly extend CollectionType<Object> instead of whatever they're a collection of.
  • The above-mentioned initializer error.
  • When the classes are referred to in code, they're replaced with Object.
  • The buggy behavior applies to array-of types in addition to additionalProperties.

@helango-seabiscuit
Copy link

Any update on the issue? Is this fixed? Facing same issue

@Praveen2518
Copy link

It is slightly different issue in JAX-RS. I am working on v4.3.1 and OpenAPI v2

It generates Message.java but the pojo where messages are included does not have type information. This generates a compile error.

private Message messages = new HashMap<>();

I traced it to below code on pojo.mustache

private {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};

I am not sure how HashMap<> is getting substituted here. Please let me know if you want me to create a separate issue for this

Hi,
Did you solve this issue how to remove that hashMap how it is substituting.i am also stuck at this problem and I am using open api 5.1.0 version

@JaquelineP
Copy link

Any updates?
I tried it with openapi-generator-maven-plugin 5.4.0. and get an compile time error. When adding an item to the hashmap it tries to initialize the Map. Here's a snippet of the generated code:
image

@MarcelHoell
Copy link

Any updates? I tried it with openapi-generator-maven-plugin 5.4.0. and get an compile time error. When adding an item to the hashmap it tries to initialize the Map. Here's a snippet of the generated code: image

I've got the same problem...

@arvindkrishnakumar-okta
Copy link
Contributor

@wing328 I am encountering the same issue. Do you recommend any workaround/solution here? I see we have something like this in .NET - any similar thing for Java?

@jehrenzweig-leagueapps
Copy link

jehrenzweig-leagueapps commented Nov 16, 2022

@MarcelHoell @arvindkrishnakumar-okta For each of you, does the non-working Java class that OpenAPI Generator produces contain extends HashMap<K, V> in the class definition? K and V can by any valid types, of course.

lburgazzoli added a commit to lburgazzoli/bf2-cos-fleetshard that referenced this issue Dec 14, 2022
The OpenAPI definition is amended and the annotation schmea replaced by
a plain object definition because of a bug in the openapi generator:

    OpenAPITools/openapi-generator#7066
lburgazzoli added a commit to bf2fc6cc711aee1a0c2a/cos-fleetshard that referenced this issue Dec 15, 2022
The OpenAPI definition is amended and the annotation schmea replaced by
a plain object definition because of a bug in the openapi generator:

    OpenAPITools/openapi-generator#7066
@AlexandKo
Copy link

Screenshot 2023-01-15 at 18 00 00

I've got the same problem. Used Gradle Plugin version 6.2.1. Any comments. Thank you.

@AlexandKo
Copy link

Update to Gradle Plugin version 6.4.0 resolved problem

@mybr4inhurts
Copy link

mybr4inhurts commented May 15, 2023

Still exists with maven plugin >6.3.0

Tested with 6.4, 6.5 and 6.6

api-spec.yaml:

        description:
          type: object
          nullable: true
          description: description of the object
          additionalProperties:
            type: string
          example:
            de: Wurst
            en: sausage

generated java code (tested with 6.4, 6.5, 6.6):

    /**
    * description of the object
    **/
    @JsonbProperty("description")
    private Map<String, String> description = ;

generated code with 6.3.0:

    /**
    * description of the object
    **/
    @JsonbProperty("description")
    private Map<String, String> description = null;

pom.xml (working config):

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.3.0</version>
    <executions>
        <execution>
            <id>generate-api</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generatorName>java</generatorName>
                <generateAliasAsModel>true</generateAliasAsModel>  <-- has no effect though, can be omitted
            </configuration>
        </execution>
    </executions>
</plugin>

@wing328
Copy link
Member

wing328 commented May 24, 2023

generated java code (tested with 6.4, 6.5, 6.6):

can you please test with the latest master? I did a test but couldn't repeat the issue. The output compiles fine.

@typekpb
Copy link

typekpb commented Jul 18, 2023

HashMap generation (https://swagger.io/docs/specification/data-models/dictionaries/) in java works in 5.0.0-beta2 version, but is broken in 6.6.0 version.

@mimkorn
Copy link

mimkorn commented Oct 11, 2023

If my understanding is correct, currently it's not possible to have additionalProperties of type object to work in the current java client generator. I do not see any intention to move this forward in the past years — is it a rarely required functionality? Because we sure need it.

@DominikBasnerSotec
Copy link

My workaround: string-replace

			<plugin>
				<groupId>io.github.floverfelt</groupId>
				<artifactId>find-and-replace-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>exec-1</id>
						<phase>process-resources</phase>
						<goals>
							<goal>find-and-replace</goal>
						</goals>
						<configuration>
							<!-- Possible replacementType values: file-contents, filenames, directory-names. To run for multiple types, pass the values as a CSV list. -->
							<replacementType>file-contents</replacementType>
							<baseDir>target/generated-sources/openapi</baseDir>
							<findRegex>private DeviceAcl acl = new HashMap&lt;>\(\);</findRegex>
							<replaceValue>private DeviceAcl acl = new DeviceAcl();</replaceValue>
							<recursive>true</recursive>
							<fileMask>.java</fileMask>
						</configuration>
					</execution>
				</executions>
			</plugin>

For each line that needs replacement, add an execution

@Jofairden
Copy link

Still having this issue in 2024. Sad to see it not picked up at all.

@lucar94
Copy link
Contributor

lucar94 commented Mar 1, 2024

I tried with 7.0.0 and 7.3.0 versions and bug seems solved (with 6.6.0 I had the problem).

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

No branches or pull requests