openapi-generator: [BUG] [JAVA] "additionalProperties" does not generate a HashMap Model, openapi: 3.0.2

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

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 23 (5 by maintainers)

Commits related to this issue

Most upvoted comments

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

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.

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

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?

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

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.

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…