[BUG] Nullable object reference produce invalid code in kotlin, php, java, typescript-fetch and probably more
Created by: etremblay
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.