OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.56k stars 6.28k forks source link

[BUG] Generator 3.3.4 does not respect Java import mapping for string refs #2858

Open chrylis opened 5 years ago

chrylis commented 5 years ago
Description

I am trying to map a custom data type to a JSON string-valued property (more or less equivalent to a custom format); Jackson will transparently handle the conversion for me in Java. However, the generator does not observe user custom mappings when a $ref type is marked as a string.

openapi-generator version

This behavior is observed with 3.3.4

OpenAPI content
# correctly identifies that EntityIdentifier is a string and produces a Map<String, String>,
# but does not use the LSP to use a custom data type
components:
  schemas:
    EntityIdentifier:
      type: string
      pattern: '^\w+:[\w\d-]+$'
    ResolveExpressionRequest:
      type: object
      properties:
        expression:
          type: string
          minLength: 1
        rootObject:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/EntityIdentifier'
# Generates a Map<String, EntityIdentifierDto>
components:
  schemas:
    EntityIdentifier:
      type: object
    ResolveExpressionRequest:
      type: object
      properties:
        expression:
          type: string
          minLength: 1
        rootObject:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/EntityIdentifier'
Command line used for generation

Invoked via Maven plugin:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>3.3.4</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/openapi/openapi.yaml</inputSpec>
                <generatorName>java</generatorName>
                <importMappings>EntityIdentifier=foo.EntityIdentifierDto</importMappings>
                <configOptions>
                  <library>resttemplate</library>
                  <java8>true</java8>
                </configOptions>
            </configuration>
        </execution>
    </executions>
  </plugin>
Steps to reproduce

As above, a $ref that is of type: string does not get overridden by the custom mapping.

chrylis commented 5 years ago

After noticing something else, I tried removing the language-specific primitive that was suggested in docs; the import mapping is the controlling variable and is both necessary and sufficient for this use case (although apparently having a completely flat namespace for the $ref types is a little concerning).

wing328 commented 5 years ago

You may also want to try the latest master (the snapshot version can be found in the README)

chrylis commented 5 years ago

With 4.0.0.beta3, this substitution fails in both cases. In the case of type: string, the Java type is java.lang.String, and with type: object, it is java.lang.Object.

chrylis commented 4 years ago

This bug is still present in 4.2.1. Specifically, it presents when either the custom type is of type: string (think something like a UUID) or when the custom type is of type: object but does not have properties defined. Only when the custom type has at least one property is the import mapping respected.

wing328 commented 4 years ago

What about using something like the following instead?

    EntityIdentifier:
      type: string
      format: EntityIdentifier

I remember it works for type mapping but not sure about import mapping. Please give it a try.