Skip to content

[BUG] Golang: Values in generated map for OpenAPI object aren't nullable #24264

Description

@rubenhoenle

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

Given the following schema in OpenAPI the generator does not actually generate a map with nullable values.

LabelPatch:
  type: object
  description: "Object that represents the labels of an object."
  additionalProperties:
    type: string
    nullable: true
  example:
    foo: "bar"
  • Actual behavior: It generates a field of type *map[string]*string.
  • Expected behavior: A field of type *map[string]*string is generated.
openapi-generator version

I'm using v7.22.0, but the issue is also present on the latest main branch.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Minimal Example API
  version: 1.0.0

paths:
  /resources/{id}:
    patch:
      summary: Update a resource
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        description: Payload to update resource attributes, including labels.
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ResourceUpdate'
      responses:
        '200':
          description: Resource updated successfully
components:
  schemas:
    ResourceUpdate:
      type: object
      description: Payload for updating a resource.
      properties:
        description:
          type: string
          description: A new description for the resource.
        labels:
          $ref: '#/components/schemas/LabelPatch'
    LabelPatch:
      type: object
      description: "Object that represents the labels of an object."
      additionalProperties:
        type: string
        nullable: true
      example:
        foo: "bar"
Generation Details
  • I'm using the Golang client generator
Steps to reproduce
# build openapi generator from source
./mvnw clean install
# generate go code
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate --generator-name go --input-spec openapi.yaml --output "output"

The generated code in output/model_resource_update.go looks like this:

// ResourceUpdate Payload for updating a resource.
type ResourceUpdate struct {
	// A new description for the resource.
	Description *string `json:"description,omitempty"`
	// Object that represents the labels of an object.
	Labels *map[string]string `json:"labels,omitempty"`
}
Related issues/PRs

Couldn't find any.

Suggest a fix

The generated code should look like this:

// ResourceUpdate Payload for updating a resource.
type ResourceUpdate struct {
	// A new description for the resource.
	Description *string `json:"description,omitempty"`
	// Object that represents the labels of an object.
-	Labels *map[string]string `json:"labels,omitempty"`
+	Labels *map[string]*string `json:"labels,omitempty"`
}

Can be done by adjusting the AbstractGoCodegen.getTypeDeclaration implementation. Will contribute it soon :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    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