OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Go] XML array is always present even if nullable and not required (missing pointer on slice)

Open
#17,687 0 comments 1 reaction 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

Description

When I generate an array with nullable set to true, we don't have a pointer on the generated slice even if the field is not required.
During marshalling of the generated struct, if the slice is empty:

  • with JSON, the omitempty option allows to have the deletion of the field containing the slice
  • with XML, if the slice is empty, the field always is present.

With the below swagger.yaml, we obtain:
actual struct:

type Command struct {
	// List of Items
	Structmember []Item `json:"struct>member,omitempty" xml:"struct>member"`
}

expected struc:

type Command struct {
	// List of Items (with a pointer)
	Structmember *[]Item `json:"struct>member,omitempty" xml:"struct>member"`
}

And with this test, we see that, without pointer, we obtain a different behavior between JSON and XML:

package model

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"testing"
)

func TestNewCommand(t *testing.T) {
	cmd := Command{}

	buff, _ := json.Marshal(cmd)
	fmt.Println("- With JSON: ", string(buff))
	buff, _ = xml.Marshal(cmd)
	fmt.Println("- With XML: ", string(buff))
}

actual output:

- With JSON:  {}
- With XML:  <Command><struct></struct></Command>

expected output:

- With JSON:  {}
- With XML:  <Command></Command>
openapi-generator version

openapitools/openapi-generator-cli:v7.2.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: mock
  version: 0.0.1
paths:
  /mock:
    post:
      operationId: post
      requestBody:
        content:
          application/xml:
            schema:
              $ref: '#/components/schemas/Command'
        required: true
      responses:
        '200':
          description: Successful response
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/Response'
components:
  schemas:
    Command:
      type: object
      properties:
        struct>member:
          type: array
          nullable: true
          description: List of Items
          items:
            $ref: "#/components/schemas/Item"
    Response:
      type: object
      properties:
        TYPE:
          type: string
          description: command
      required:
        - TYPE
    Item:
      type: object
      description: contains a member field
      properties:
        name:
          type: string
        value:
          type: string
      required:
        - name
        - value
Generation Details
> docker run --rm -it -v /test/tmp:/tmp -w /tmp openapitools/openapi-generator-cli:v7.2.0 generate -i /tmp/swagger.yaml --strict-spec true --additional-properties=useOneOfDiscriminatorLookup=true,withXml=true -p enumClassPrefix=true -g go -o /tmp/generated/model/ --package-name model --git-repo-id tmp/tmp/generated/model/
Steps to reproduce

0°) put the swagger.yaml into /test/tmp + mkdir -p generated/model
1°) run the generation command line from the
2°) run the provided go test

Linked Issues

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 with the provided swagger.yaml and generation command, then run the provided Go marshalling test to reproduce the generated Command output. Trace the Go generator's handling of nullable, non-required array properties with XML enabled; done when the generated field is *[]Item and empty XML omits struct>member while JSON remains omitted.

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
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.