Skip to content

[BUG][Java][jersey2] Incorrect import mapping for arrays #7018

@ioannisioannou16

Description

@ioannisioannou16

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Hello,

I am trying to use importMappings with arrays but it doesn't work properly. I have the following example:

openapi: 3.0.0
info:
  title: Sample API
  description: API description
  version: 1.0.0
paths:
  /test:
    get:
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MyType1'
components:
  schemas:
    MyType1:
      type: object
      properties:
        arrayProperty:
          $ref: '#/components/schemas/MyType2'
        otherProperty:
          $ref: '#/components/schemas/MyType4'
    MyType2:
      type: array
      items:
        $ref: '#/components/schemas/MyType3'
    MyType3:
      type: string
    MyType4:
      type: string

and I defined the following importMappings:

<importMappings>
    <importMapping>MyType3=com.example.MyType3</importMapping>
    <importMapping>MyType4=com.example.MyType4</importMapping>
</importMappings>

It replaces successfully the MyType4 but for the MyType3 it generates a String as the type of the array.

Generated response types:

private List<String> arrayProperty = null;
private com.example.MyType4 otherProperty;

I was expecting the response to be List<com.example.MyType3> for the arrayProperty.

openapi-generator version

openapi-generator-maven-plugin
5.0.0-beta
jaxrs-jersey

Related issues/PRs

I think this issue #3589 is related. It was for simple types though. Here is for arrays.

Suggest a fix/enhancement

I think the following changes fix the issue.

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
index bbf9a3004a..ae27927211 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
@@ -759,7 +759,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
     public String getTypeDeclaration(Schema p) {
         if (ModelUtils.isArraySchema(p)) {
             Schema<?> items = getSchemaItems((ArraySchema) p);
-            return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items)) + ">";
+            return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items, importMapping)) + ">";
         } else if (ModelUtils.isMapSchema(p) && !ModelUtils.isComposedSchema(p)) {
             // Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
             // additionalproperties: true
diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java
index 0a694cffd6..b4e6fbfa53 100644
--- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java
@@ -423,6 +423,16 @@ public class AbstractJavaCodegenTest {
         Assert.assertEquals(defaultLocalDate, LocalDate.parse(defaultValue));
     }
 
+    @Test
+    public void getTypeDeclarationGivenStringImportMappingTest() {
+        final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
+        codegen.importMapping().put("MyStringType", "com.example.foo");
+        codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("MyStringType", new StringSchema())));
+        Schema<?> schema = new ArraySchema().items(new Schema().$ref("#/components/schemas/MyStringType"));
+        String defaultValue = codegen.getTypeDeclaration(schema);
+        Assert.assertEquals(defaultValue, "List<com.example.foo>");
+    }
+
     @Test
     public void getTypeDeclarationTest() {
         final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();

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