OpenAPITools / OpenAPITools/openapi-generator

[BUG][Go] Query parameter with oneOf enum doesn't work with defaultValue

Open
#16,574 3 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

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

When using an enum schema with oneOf and default, the new default value handling from #15126 causes the generated code to not build.

openapi-generator version

master @ 8b15d482

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  description: 'Minimium viable reproduction for issue 16574'
  version: 1.0.0
  title: OpenAPI Petstore
paths:
  /pet/findByStatus:
    get:
      tags:
        - pet
      summary: Finds Pets by status
      description: Find pets by a single status
      operationId: findPetsByStatus
      parameters:
        - name: status
          in: query
          description: Status value that needs to be considered for filter
          schema:
            default: "available"
            oneOf:
              - $ref: '#/components/schemas/StatusType'
      responses:
        '200':
          description: successful operation
          content:
            application/xml:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid status value
components:
  schemas:
    StatusType:
      type: string
      enum:
        - available
        - pending
        - sold
    Pet:
      title: a Pet
      description: A pet for sale in the pet store
      type: object
      required:
        - name
        - photoUrls
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
          example: doggie
        status:
          $ref: '#/components/schemas/StatusType'
      xml:
        name: Pet
Generation Details
> java -jar .\openapi-generator-cli.jar generate -g go -o petstore -i petstore.yaml  --package-name petstore -t C:\repos\openapi-generator\modules\openapi-generator\src\main\resources\go

Generates the following invalid go:

	if r.status != nil {
		parameterAddToHeaderOrQuery(localVarQueryParams, "status", r.status, "")
	} else {
		var defaultValue interface{} = available
		r.status = &defaultValue
	}
> go build
.\api_pet.go:80:34: undefined: available
Steps to reproduce

A query parameter using a schema with default and oneOf:

      parameters:
        - name: status
          in: query
          description: Status value that needs to be considered for filter
          schema:
            default: "available"
            oneOf:
              - $ref: '#/components/schemas/StatusType'
Related issues/PRs

Issue introduced by #15126

Suggest a fix

I tried running the generation with defaultValue=false but that didn't stop the default value code from being generated:

java -jar .\openapi-generator-cli.jar generate -g go -o petstore-i petstore.yaml  --additional-properties="defaultValue=false" --package-name petstore -t C:\repos\openapi-generator\modules\openapi-generator\src\main\resources\go

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

Inspect the Go generator templates under modules/openapi-generator/src/main/resources/go and trace default handling for the oneOf query parameter. Reproduce the issue with the provided OpenAPI YAML and generation command, then run go build on generated api_pet.go. Done means the generated default is a valid Go value and the client builds successfully.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.