OpenAPITools / OpenAPITools/openapi-generator
[BUG] Nullable object reference produce invalid code in kotlin, php, java, typescript-fetch and probably more
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
To represent nullable object property, openapi recommend using a oneOf constuct. In version 3.0 in can be done with the nullable property, and in 3.1, with the null type.
3.0
propertyName:
nullable: true
oneOf:
- $ref: '#/components/schemas/PropertyType'
3.1
otherPropertyName:
oneOf:
- 'null'
- $ref: '#/components/schemas/PropertyType'
In kotlin, the generatated type name is incorrect : OneOfLessThanPropertyTypeGreaterThan
@Json(name = "propertyName")
val propertyName: OneOfLessThanPropertyTypeGreaterThan? = null,
In php, the code is ok but the annotations produce an invalid class name : OneOfPropertyType|null
/**
* Sets property_name
*
* @param OneOfPropertyType|null $property_name property_name
*
* @return self
*/
public function setPropertyName($property_name)
{
$this->container['property_name'] = $property_name;
return $this;
}
In java the type is also wrong
public OneOfPropertyType getPropertyName() {
return propertyName;
}
public void setPropertyName(OneOfPropertyType propertyName) {
this.propertyName = propertyName;
}
In typescipt-fetch it produces a different result with 3.0 or 3.1 syntax
// 3.0
/**
*
* @type {PropertyType}
* @memberof ModelWithNullableObjectProperty
*/
propertyName?: PropertyType | null;
// 3.1
/**
*
* @type {Null | PropertyType}
* @memberof ModelWithNullableObjectProperty
*/
otherPropertyName?: Null | PropertyType | null;
openapi-generator version
master - 5.2.1
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: 'Title'
version: latest
paths:
'/':
get:
operationId: operation
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/ModelWithNullableObjectProperty'
components:
schemas:
ModelWithNullableObjectProperty:
properties:
propertyName:
nullable: true
oneOf:
- $ref: '#/components/schemas/PropertyType'
otherPropertyName:
oneOf:
- 'null'
- $ref: '#/components/schemas/PropertyType'
PropertyType:
properties:
foo:
type: string
Generation Details
docker run --rm -v ${PWD}:/local --user "${USER_ID}:${GROUP_ID}" openapitools/openapi-generator-cli:latest generate -i /local/model.yaml -g kotlin -o /local/kotlin
docker run --rm -v ${PWD}:/local --user "${USER_ID}:${GROUP_ID}" openapitools/openapi-generator-cli:latest generate -i /local/model.yaml -g php -o /local/php
docker run --rm -v ${PWD}:/local --user "${USER_ID}:${GROUP_ID}" openapitools/openapi-generator-cli:latest generate -i /local/model.yaml -g java -o /local/java
docker run --rm -v ${PWD}:/local --user "${USER_ID}:${GROUP_ID}" openapitools/openapi-generator-cli:latest generate -i /local/model.yaml -g typescript-fetch -o /local/typescript
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/2090
Suggest a fix
I went deep into the source code to understand how nullable types are handled.
ModelUtils.isNullable seems to have some logic to handle this but I'm unable to make an unit test pass in this code path.
I'm willing to contribute but may need some guidance.
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
Start with the provided OpenAPI declaration and Docker generation commands, then inspect ModelUtils.isNullable, which the report identifies as relevant to nullable handling. Regenerate the Kotlin, PHP, Java, and typescript-fetch outputs and verify that nullable object references produce valid type names and annotations in each generator.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, php, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100