OpenAPITools / OpenAPITools/openapi-generator

[BUG] Golang code - issue with Unset() method for Nullable object

Open
#11,693 0 comments 0 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

As of version 5.4.0, Golang generated code appears to not compile correctly when using the --generate-alias-as-model option. Specifically, when referencing a schema within an array, the generated code now no longer uses pointer variables, which causes a compile error in the Unset() method.

Suppose the following schema snippet:

components:
  schemas:
    webhook:
      type: object
      properties:
        somefield:
          type: string
    webhook.list:
      description: List of webhooks.
      type: array
      items:
        $ref: '#/components/schemas/webhook'

This generates the following Go code (relevant parts shown):

// WebhookList List of webhooks.
type WebhookList struct {
	Items []Webhook
}

type NullableWebhookList struct {
	value WebhookList
	isSet bool
}

func (v *NullableWebhookList) Unset() {
	v.value = nil // This line causes a compilation error as assigning nil to a struct
	v.isSet = false
}

Previously the value variable was a pointer i.e. value *WebhookList as opposed to value WebhookList), so the nil assignment in Unset() worked. However this behaviour has changed in 5.4.0.

openapi-generator version

5.4.0
Appears to be a regression, this was working fine in 5.3.0

OpenAPI declaration file content or url
---
openapi: 3.0.1
info:
  title: Test
  version: 20200511-01

paths:
  /test:
    post:
      tags:
        - Test
      summary: Test
      requestBody:
        description: Test
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/webhook.list'
        required: true
      responses:
        200:
          description: The request has been accepted.
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string

components:
  schemas:
    webhook:
      type: object
      properties:
        somefield:
          type: string
    webhook.list:
      description: List of webhooks.
      type: array
      items:
        $ref: '#/components/schemas/webhook'
Generation Details

Option --generate-alias-as-model is used to generate the above Go 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

Reproduce generation with the supplied OpenAPI schema and the --generate-alias-as-model option, comparing versions 5.3.0 and 5.4.0. Inspect the generated NullableWebhookList and its Unset() method; done means the generated Go code compiles and Unset() works when the value is a struct.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.