Skip to content

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

@wguo-github

Description

@wguo-github

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions