Skip to content

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

Open

Description

@chrylis
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.

Activity

  1. changed the title [-][BUG] Generator 3.3.4 does not respect language-specific primitive for string refs[/-] [+][BUG] Generator 3.3.4 does not respect Java import mapping for string refs[/+] on May 10, 2019
  2. chrylis commented on May 10, 2019

    @chrylis
    Author

    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).

  3. wing328 commented on May 10, 2019

    @wing328
    Member

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

  4. chrylis commented on May 10, 2019

    @chrylis
    Author

    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.

  5. added a commit that references this issue on Oct 19, 2019
    4332d75
  6. chrylis commented on Nov 17, 2019

    @chrylis
    Author

    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.

  7. wing328 commented on Nov 17, 2019

    @wing328
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions