kokuwaio/micronaut-openapi-codegen

Mapping of array of unique items to Java set is incomplete

kiwi-oss opened this issue · 6 comments

The schema definition of an array with unique strings correctly generates a Java set of strings:

MapWithArraysOfUniqueStrings:
  type: object
  additionalProperties:
    type: array
    items:
      type: string
    uniqueItems: true
private java.util.Map<String, Set<java.lang.String>> mapField;

However, Set is neither fully qualified nor is it imported. This leads to compile errors like this one:

SomeClass.java:42: error: cannot find symbol
        private java.util.Map<String, Set<java.lang.String>> mapField;
                                      ^
  symbol:   class Set
  location: class SomeClass

I have tried to add the following import mapping in build.gradle:

openApiGenerate {
	generatorName = "micronaut"
	// ...
	importMappings = [Set: "java.util.Set"]
}

Unfortunately, then I get another error:

Cannot set the value of a property of type java.util.Map using an instance of type java.util.ArrayList.

Edit: I corrected an error in the Gradle syntax. Fixing it resulted again in the errors about the missing symbol.

I assume all of this happens because the mapping of unique-item arrays is not complete:

  1. It has to be mapped to java.util.Set
  2. HashSet has to be instantiated instead of ArrayList

In order to get the fully qualified name for 'Set', you need to set the 'fullJavaUtil' config-option to true(see https://openapi-generator.tech/docs/generators/java/).
I'm unsure about your second point, can you provide a test to reproduce this?

Thanks, I'll check how I can set that option and report back. After a quick look, I couldn't find it in the Gradle plugin.

I found a mistake in my importMappings configuration. It should be [Set: "java.util.Set"] with a colon instead of a comma.
However, it has no effect, I still get the errors about the missing Set symbol.

You can set generator specific configurations(like fullJavaUtil) via the configOptions-map of your plugin.

That did the trick, thank you!

I still wonder why the import mapping didn't help, though. Or why contrary to the other types like Map or String, this one is not generated with a fully qualified name.

In other words, should the issue be kept open despite the fullJavaUtil solution?

I think the issue should be closed. It works as expected for all types that are generated directly by the java-base generator(and therefore is influenced by the fullJavaUtil option). This is also true for Map and String occurences. But there are some cases where the types are hard-coded inside the mustache-files(either in the micronaut-generator files or in the java-generator files) and those are usually hardcoded with fully qualified name, since they cannot by directly influenced by the generator.

I see, the hard-coded types explain my second point.

Regarding the first one, maybe I misunderstood how the import mapping works. If you can also help me with this one, that would be great. Otherwise I'll try to find more information about that.

In any case, I'll close the issue. Thanks for the explanation!