Search code examples
javaswaggeropenapiopenapi-generatorswagger-codegen

How to resolve `reference to Type is ambiguous error` in OpenAPI Generator Maven Plugin?


My openapi-generator-maven plugin configuration is like below

<plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>7.1.0</version>
        <configuration>
          <generateSupportingFiles>true</generateSupportingFiles>
          <generatorName>spring</generatorName>
          <generateApiDocumentation>true</generateApiDocumentation>
          <generateModelDocumentation>true</generateModelDocumentation>
          <globalProperties>
            <skipFormModel>false</skipFormModel>
          </globalProperties>
          <!-- Duplicate classes being generated due to https://github.com/OpenAPITools/openapi-generator/issues/2701 -->
          <typeMappings>
            string+binary=StreamingResponseBody,ExpensePartnerConfig1=ExpensePartnerConfig,FeatureConfig1=FeatureConfig,Application1=Application,PaymentSourceInfo1=PaymentSourceInfo,CostCenter1=CostCenter,OAuthPartnerConfig1=OAuthPartnerConfig,DateTimeRange1=DateTimeRange,TmcInfo1=TmcInfo,Company1=Company,SupportConfig1=SupportConfig,RewardsProgram1=RewardsProgram,CompanyBasicInfo1=CompanyBasicInfo,UserPersonalInfo1=UserPersonalInfo,PnrData1=PnrData,Agent1=Agent,DocumentMetadata1=DocumentMetadata,Document1=Document,BasicTripInfo1=BasicTripInfo,VendorProgramPaymentDescriptor1=VendorProgramPaymentDescriptor,VendorProgramPaymentDescriptorWrapper1=VendorProgramPaymentDescriptorWrapper,WhiteLabelConfig1=WhiteLabelConfig,AirBookTravelerInfo1=AirBookTravelerInfo,UserGroup1=UserGroup,Question1=Question,PaymentSourceMetadata1=PaymentSourceMetadata,EventLocation1=EventLocation,EventUserRsvp1=EventUserRsvp,FormOfPayment1=FormOfPayment,PaymentSourceFilter1=PaymentSourceFilter,PaymentSourceTravelType1=PaymentSourceTravelType,PaymentSourceMappings1=PaymentSourceMappings,PaymentSourceMapping1=PaymentSourceMapping,PaymentSourceSpendType1=PaymentSourceSpendType,PaymentSourceAttributes1=PaymentSourceAttributes,LegalEntityFilter1=LegalEntityFilter,CompanyFilter1=CompanyFilter,CountryFilter1=CountryFilter,CostCenterFilter1=CostCenterFilter,PaymentSourceDepartmentFilter1=PaymentSourceDepartmentFilter,TravelerEventSummary1=TravelerEventSummary,AccessTypeAttributes1=AccessTypeAttributes,TravelTypeFilter1=TravelTypeFilter,PrivacyNotice1=PrivacyNotice
          </typeMappings>
          <importMappings>
            StreamingResponseBody=org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
          </importMappings>
          <configOptions>
            <oas3>true</oas3>
            <useTags>true</useTags>
            <withSeparateModelsAndApi>true</withSeparateModelsAndApi>
            <delegatePattern>true</delegatePattern>
            <withInterfaces>true</withInterfaces>
            <library>spring-boot</library>
            <dateLibrary>java8</dateLibrary>
            <useSpringfox>false</useSpringfox>
            <useSpringController>true</useSpringController>
            <openApiNullable>false</openApiNullable>
            <modelPropertyNaming>camelCase</modelPropertyNaming>
            <useJakartaEe>true</useJakartaEe>
          </configOptions>
          <additionalProperties>removeEnumValuePrefix=false</additionalProperties>
        </configuration>
        <executions>
          <execution>
            <id>generate-client</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/target/classes/generated/yaml/apis/api/Api.yaml
              </inputSpec>
              <generatorName>java</generatorName>
              <generateApiDocumentation>false</generateApiDocumentation>
              <modelNamePrefix>Spotnana</modelNamePrefix>
              <generateModels>true</generateModels>
              <generateApis>true</generateApis>
              <library>okhttp-gson</library>
              <configOptions>
                <library>okhttp-gson</library>
              </configOptions>
              <additionalProperties>removeEnumValuePrefix=true</additionalProperties>
              <output>${project.build.directory}/generated-sources/openapi/client</output>
            </configuration>
          </execution>
        </executions>
      </plugin>

I am getting the below error during generation. Can someone suggest how can I get around this?

[ERROR] /Users/debrajmanna/code/java/github/spotnana/src/java/openapi-generator/target/generated-sources/openapi/client/src/main/java/org/openapitools/client/model/ServiceChargeRecord.java:[76,10] error: reference to Type is ambiguous
  both enum org.openapitools.client.model.Type in org.openapitools.client.model and interface java.lang.reflect.Type in java.lang.reflect match
[ERROR] /Users/debrajmanna/code/java/github/spotnana/src/java/openapi-generator/target/generated-sources/openapi/client/src/main/java/org/openapitools/client/model/ServiceChargeRecord.java:[164,41] error: reference to Type is ambiguous

Solution

  • I had one OpenAPI schema like below

    Type:
          type: string
          title: ServiceType
          description: Type of service
          enum:
            - BOOKING
            - CONTACT
    

    This was creating the conflict. I changed my schema like below which solved the issue for me.

    ServiceType:
          type: string
          title: ServiceType
          description: Type of service
          enum:
            - BOOKING
            - CONTACT
    

    Another option suggested in openapi-generator slack group is to use model name mapping. Cross-posting the answer

    you can use the model name mapping option to map Type to something else (e.g. ServiceType).

    ref: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#name-mapping