Open alex-korobko opened 5 years ago
@alex-korobko thanks for reporting the issue and offering help to fix it.
I found the following in the mustache template:
modules/openapi-generator/src/main/resources/go/model.mustache: {{name}} {{#isNullable}}*{{/isNullable}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
so it seems like nullable is supported but not working somehow for nullable model.
Can you test with nullable string or integer instead to see if that works? If it works, we can isolate the problem to nullable model only.
@wing328 Sorry for the late response.
As I understand (I could be wrong) the mustache file is applied after the models are generated. And I see in the debugModels
output that isNullable
flag is set to false
. So in this case the mustache file has nо effect.
I.e. despite the fact that in spec I defined NullableRefProperty
as nullable: true
NullableRefProperty:
nullable: true
description: Just an object that could be null
$ref: '#/components/schemas/NullableRef'
The output of debugModels
(see my comment above for details) flag shows that isNullable
for the NullableRefProperty
is returned as false
"isNullable" : false,
and therefore
modules/openapi-generator/src/main/resources/go/model.mustache: {{name}} {{#isNullable}}*{{/isNullable}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
is not applied.
Any update on that, guys?
I think the generator is looking at the referenced definition to assign the nullable
property (https://github.com/ackintosh/openapi-generator-1/blob/10e42461f5099d6c63e4592299f1baca93711f12/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L2248)
Workaround that works for me is by defining the referenced definition as nullable: eg in the example
NullableRef:
type: object
nullable: true
properties:
Name:
type: string
nullable: true
description: Just a name to test in nullable ref
@stevenvdr Won't work in the case when referenced definition used in multiple places, with and without nullable.
It seems this is somehow related to https://github.com/OAI/OpenAPI-Specification/issues/1368?
Unfortunately, the following workaround does not work here:
"properties": {
"name": { "type": "string" },
"dataFork": { "$ref": "#/components/schemas/Fork" },
"resourceFork": {
"nullable": true,
"anyOf": [ { "$ref": "#/components/schemas/Fork" } ]
},
Bug Report Checklist
Description
There are no pointers generated for the references with
nulllable: true
.openapi-generator version
The
latest
Docker image. I tried Docker images tagged asv4.0.0
andv4.0.1
as well.OpenAPI declaration file content or url
Here is the Gist I created to reproduce the issue : https://gist.github.com/alex-korobko/e5029d746e3c5631af9ab43319761d0f
Command line used for generation
I used the following generation parameters:
Steps to reproduce
Use the spec and generate the models using the command line I provided. The
NullableRefProperty
property is not generated as a pointer:It looks like there is no generation for the reference property
NullableRefProperty
despite the fact that the property do have thenullable: true
in spec:In the debug output produced when the
debugModels
flag is set it shows that theisNullable
property is not set for theNullableRefProperty
values :Related issues/PRs
The issue is different from https://github.com/OpenAPITools/openapi-generator/issues/2119 , the inline defined objects are got generated with references if nullable is set to true (see the spec for details)
Suggest a fix
I'd be glad to help fixing the issue, I have some experience in Java programming that is used to write the parser if I'm not mistaking. I assume the issue happens during the parsing the spec, as the variable properties that sent to the code generator already have
"isNullable" : false,
despite the fact that in spec theNullableRefProperty
property hasnullable: true
property set. It would be great if you guys provide me some initial guidance where to start in code.