OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Kotlin-Client] Strange DefaultApi import when mapping custom types
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
When generating a Kotlin client with an API that uses custom types, I would prefer to use my own data classes instead of the ones generated by OpenAPI.
However, when specifying these classes in the configuration, the import clauses in DefaultApi are wrong, and I am not sure if my configuration is wrong or if there is a problem in OpenApi.
I have tried different combinations of adding or omitting the full package in the configuration both for typeMapping and for importMapping, and the results are varied (e.g., a configuration adds the wrong import, another creates the data class even with the mapping specified, another collapses the package and class name removing the dots...).
Let me know if there is a correct combination of typeMapping and importMapping that eludes me, to obtain the expected result, or if there is a problem with OpenApi.
openapi-generator version
The problem was found with version 4.2.0, and it remains if using master.
OpenAPI declaration
openapi: 3.0.1
paths:
/operations/{operationId}:
get:
summary: Retrieve the descriptor of an operation.
operationId: getOperation
parameters:
- name: operationId
in: path
description: Id of the operation.
required: true
schema:
type: integer
format: int32
example: 1
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/CustomType'
404:
description: Operation not found
components:
schemas:
CustomType:
type: object
properties:
name:
type: string
format: uuid
Configuration
{
"packageName": "some.package",
"groupId": "mygroup",
"artifactId": "myclient",
"artifactVersion": "1.0",
"enumPropertyNaming": "UPPERCASE",
"serializationLibrary": "gson",
"serializableModel": "true",
"dateLibrary": "java8",
"collectionType": "list",
"library": "jvm-okhttp3",
"typeMappings": {
"CustomType": "CustomType"
},
"importMappings": {
"CustomType": "some.other.package.CustomType"
}
}
Command line used for generation
java -jar openapi-generator-cli.jar generate -g kotlin -o xxx -i sample-api.yaml -c sample-config.json --skip-validate-spec
Expected behavior
Import declaration:
import some.other.package.CustomType
Method signature:
fun getOperation(operationId: kotlin.Int) : CustomType
Actual behavior
Import declaration:
import some.package.models.some.other.package.CustomType
Method signature:
fun getOperation(operationId: kotlin.Int) : some.other.package.CustomType
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the supplied sample OpenAPI declaration, configuration, and Kotlin generation command. Start by tracing how the Kotlin generator applies typeMappings and importMappings, then compare the generated DefaultApi import and method signature with the expected behavior. Done means the custom type is not generated, the import is exact, and the return type is CustomType.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100