OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Go] The nullable property value has no effect to ref

Open
#3,159 6 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

There are no pointers generated for the references with nulllable: true.

openapi-generator version

The latest Docker image. I tried Docker images tagged as v4.0.0 and v4.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

openapi: 3.0.1
info:
  version: 1.0.0
  title: Test spec
  description: Trying to find out if nullable affects output for refs in code generation for Go
  contact:
    name: Alex Korobko
    email: korobko.alex@gmail.com
servers:
  - url: https://my.cool.server.com/api/v1
paths:
  /GetHealthCheck:
    get:
      summary: Health check endpoint
      responses:
        200:
          description: Returns successful result if the instance started successfully
          content:
            application/json:
              schema:
                type: object
                $ref: '#/components/schemas/HealthCheckResult'
components:
  schemas:
    HealthCheckResult:
      type: object
      properties:
        NullableRefProperty:
          nullable: true
          description: Just an object that could be null
          $ref: '#/components/schemas/NullableRef'
        NullableObjProperty:
          type: object
          nullable: true
          description: Nullable object directly in spec
          properties:
              NullableName:
                type: string
                nullable: true
                description: Nullable property in object
    NullableRef:
      type: object
      properties:
        Name:
          type: string
          nullable: true
          description: Just a name to test in nullable ref

Command line used for generation

I used the following generation parameters:

docker run  \
        --volumes-from spec-image \
        openapitools/openapi-generator-cli:latest generate \
        -t /$DOCKER_VOLUME_NAME/ \
        -g go \
        -i /$DOCKER_VOLUME_NAME/spec.yaml \
        -DdebugModels \
        -DdebugOperations \
        -DenablePostProcessFile=true \
        -Dmodels \
        -DmodelDocs=false\
        -DpackageName=models\
        -o /$DOCKER_VOLUME_NAME/$GENERATE_DIR
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:

/*
 * Test spec
 *
 * Trying to find out if nullable affects output for refs in code generation for Go
 *
 * API version: 1.0.0
 * Contact: korobko.alex@gmail.com
 * Generated by: OpenAPI Generator (https://openapi-generator.tech)
 */

package models

type HealthCheckResult struct {
	NullableRefProperty NullableRef `json:"NullableRefProperty,omitempty"`
	NullableObjProperty *HealthCheckResultNullableObjProperty `json:"NullableObjProperty,omitempty"`
}

It looks like there is no generation for the reference property NullableRefProperty despite the fact that the property do have the nullable: true in spec:

  NullableRefProperty:
          nullable: true

In the debug output produced when the debugModels flag is set it shows that the isNullable property is not set for the NullableRefProperty values :

    "vars" : [ {
      "openApiType" : "NullableRef",
      "baseName" : "NullableRefProperty",
      "complexType" : "NullableRef",
      "getter" : "getNullableRefProperty",
      "setter" : "setNullableRefProperty",
      "dataType" : "NullableRef",
      "datatypeWithEnum" : "NullableRef",
      "name" : "NullableRefProperty",
      "defaultValueWithParam" : " = data.NullableRefProperty;",
      "baseType" : "NullableRef",
      "example" : "null",
      "jsonSchema" : "{\n  \"$ref\" : \"#/components/schemas/NullableRef\"\n}",
      "exclusiveMinimum" : false,
      "exclusiveMaximum" : false,
      "hasMore" : true,
      "required" : false,
      "secondaryParam" : false,
      "hasMoreNonReadOnly" : false,
      "isPrimitiveType" : false,
      "isModel" : true,
      "isContainer" : false,
      "isString" : false,
      "isNumeric" : false,
      "isInteger" : false,
      "isLong" : false,
      "isNumber" : false,
      "isFloat" : false,
      "isDouble" : false,
      "isByteArray" : false,
      "isBinary" : false,
      "isFile" : false,
      "isBoolean" : false,
      "isDate" : false,
      "isDateTime" : false,
      "isUuid" : false,
      "isUri" : false,
      "isEmail" : false,
      "isFreeFormObject" : false,
      "isListContainer" : false,
      "isMapContainer" : false,
      "isEnum" : false,
      "isReadOnly" : false,
      "isWriteOnly" : false,
      "isNullable" : false,
      "isSelfReference" : false,
      "vendorExtensions" : { },
      "hasValidation" : false,
      "isInherited" : false,
      "nameInCamelCase" : "NullableRefProperty",
      "nameInSnakeCase" : "NULLABLE_REF_PROPERTY",
      "isXmlAttribute" : false,
      "isXmlWrapped" : false,
      "datatype" : "NullableRef",
      "iexclusiveMaximum" : false
    },
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 the NullableRefProperty property has nullable: true property set.
It would be great if you guys provide me some initial guidance where to start in code.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided OpenAPI declaration and Go generator command, using the generated debugModels output to confirm that NullableRefProperty has isNullable set to false. Trace where nullable properties on $ref schemas are parsed and passed into the Go model template; done means the generated NullableRefProperty is a pointer and the existing inline nullable property behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, java
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.