Skip to content

[BUG] [JavaJaxRS] Combining modelNameSuffix and import-mappings #11478

Description

@mickroll

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

We use <modelNameSuffix>TO</modelNameSuffix> to distinct all our generated models from our internal models and prevent name collissions. In our Spec, we define our Ids as follows (in an extra file id.json):

      "ExampleId": {
        "description": "The example Id.",
        "type": "string",
        "format": "uuid",
        "example": "53b1f9fe-44e1-11f0-1122-336652440000"
      }

The spec then references these id types:

      "ExampleWrapper": {
        "type": "object",
        "required": ["id", "name"],
        "properties": {
          "id": {
            "$ref": "id.json#/components/schemas/ExampleId"
          },
          "name": {
            "type": "string"
          }
        }
      }

To prevent OpenAPI-Generator from generating ExampleId-classes, we define a mapping configuration via <configurationFile>configuration.json</configurationFile>:

{
    "importMappings": {
        "ExampleId": "com.example.model.id.ExampleId"
    }
}

The referenced class ExampleId already exists of course.

All of this worked in 5.3.1 an lead to sources like this:

package com.example.api.generated.model;
[...]
import com.example.model.id.ExampleId;
[...]
public class ExampleWrapperTO {
[...]
  private @Valid ExampleId id;
  private @Valid String name;
[...]
}

With 5.4.0 this is generated:

package com.example.api.generated.model;
[...]
import com.example.api.generated.model.ExampleIdTO;
[...]
public class ExampleWrapperTO {
[...]
  private @Valid ExampleIdTO id;
  private @Valid String name;
[...]

Note: a ExampleIdTO is not generated! (we don't want it to, just a note)

I tracked the changed behaviour to 33bce99#r65722393 but as the PR says, this change is intended. Was it really, given the example above? Or was using modelNamePrefix/modelNameSuffix out of scope for that PR?

Workaround

If i change our configuration.json to the following, valid sources are generated:

{
    "typeMappings": {
        "ExampleIdTO": "ExampleId",
    },
    "importMappings": {
        "ExampleId": "com.example.model.id.ExampleId",
        "ExampleIdTO": "com.example.model.id.ExampleId",
    }
}

But this seems very redundant and partly wrong to me. If i leave out any of the three definitions, it stops working:

  • leave out typeMappings.ExampleIdTO: ExampleWrapperTO uses ExampleIdTO instead of ExampleId
  • leave out importMappings.ExampleId: ExampleWrapperTO uses UUID instead of ExampleId
  • leave out importMappings.ExampleIdTO: ExampleWrapperTO imports com.example.api.generated.model.ExampleIdTO (which doesn't exist), but not com.example.model.id.ExampleId, but uses private @Valid ExampleId id, which cannot be found
openapi-generator version

5.4.0 (worked in 5.3.1)

OpenAPI declaration file content or url

see above

Generation Details

openapi-generator-maven-plugin:5.4.0
with config:

                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/META-INF/openapi.json</inputSpec>
                            <configurationFile>${project.basedir}/src/main/openapi/generator/configuration.json</configurationFile>
                            <apiPackage>com.example.api.generated</apiPackage>
                            <modelPackage>com.example.api.generated.model</modelPackage>
                            <modelNameSuffix>TO</modelNameSuffix>
                            <generatorName>jaxrs-spec</generatorName>
                            <!-- <library>quarkus</library> would generate unnecessary Dockerfile etc. -->
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <returnResponse>false</returnResponse>
                                <sourceFolder>java</sourceFolder>
                                <useSwaggerAnnotations>false</useSwaggerAnnotations>
                                <generatePom>false</generatePom>
                                <dateLibrary>java8</dateLibrary>
                            </configOptions>
                        </configuration>
Steps to reproduce
Related issues/PRs

#11217

Suggest a fix

Revert #11217 or some of its contents?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions