[BUG] kotlin-spring generator resolves invalid package for imports form importMap
Created by: michael-wirth
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
kotlin-spring
generator resolves invalid package name when using importMappings
.
<configuration>
<apiPackage>com.example.api</apiPackage>
<importMappings>Problem=org.zalando.problem.Problem</importMappings>
</configuration>
resolves to: import com.example.model.org.zalando.problem.Problem;instead of
import org.zalando.problem.Problem;`
The generation of the Java Spring version with the same api.yml
works correctly.
openapi-generator version
4.2.3
OpenAPI declaration file content or url
# API-first development with OpenAPI
# This file will be used at compile time to generate Spring-MVC endpoint stubs using openapi-generator
openapi: '3.0.3'
info:
title: demo
version: 1.0.0
servers:
- url: http://localhost:8080/api
description: Local server
tags:
- name: Account
paths:
/accounts:
get:
tags:
- Account
operationId: listAccounts
responses:
'200':
description: List of all available accounts
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Account'
'404':
$ref: '#/components/responses/Problem'
/error:
get:
tags:
- Account
operationId: error
responses:
'200':
$ref: '#/components/responses/Problem'
components:
schemas:
Account:
type: object
properties:
id:
description: Konto Laufnummer
type: integer
format: int64
accountOwnerId:
description: Kundennummer
type: string
maxLength: 12
accountNumber:
description: Kontonummer
type: string
maxLength: 20
responses:
Problem:
description: error occurred - see status code and problem object for more information.
content:
'application/problem+json':
schema:
$ref: 'https://opensource.zalando.com/problem/schema.yaml#/Problem'
example:
url: https://zalando.com/problem/not-found
title: Resource not found
status: 404
detail: index.html not found
instance: /account/not-found
Generation Details
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<kotlin.version>1.3.72</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web-starter</artifactId>
<version>0.25.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-kotlin</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<!--
Plugin that provides API-first development using openapi-generator to
generate Spring-MVC endpoint stubs at compile time from an OpenAPI definition file
-->
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.2.3</version>
<executions>
<execution>
<id>generate-kotlin-spring</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<apiPackage>com.example.api</apiPackage>
<generateApiTests>false</generateApiTests>
<generateSupportingFiles>false</generateSupportingFiles>
<generatorName>kotlin-spring</generatorName>
<importMappings>Problem=org.zalando.problem.Problem</importMappings>
<inputSpec>${project.basedir}/src/main/resources/api.yml</inputSpec>
<modelPackage>com.example.model</modelPackage>
<skipValidateSpec>false</skipValidateSpec>
<configOptions>
<basePackage>com.example</basePackage>
<exceptionHandler>false</exceptionHandler>
<enumPropertyNaming>UPPERCASE</enumPropertyNaming>
<interfaceOnly>false</interfaceOnly>
<serviceInterface>true</serviceInterface>
<sourceFolder>src/main/kotlin</sourceFolder>
<title>${project.artifactId}</title>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Steps to reproduce
- save
pom.xml
- save
api.yml
and save tosrc/main/resources/api.yml
- run
mvn install
Related issues/PRs
Suggest a fix
Apply same logic as for the AbstractJavaCodeGen
@Override
protected boolean needToImport(String type) {
return super.needToImport(type) && !type.contains(".");
}
Alternative fix is to provide 2 import mappings. One to map the entity to the actual class name and another to prevent that the API package is used as prefix.
<importMappings>Problem=org.zalando.problem.Problem,org.zalando.problem.Problem=org.zalando.problem.Problem</importMappings>