OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO] go-server does not correctly handle optional query params for boolean and string array types

Open
#9,709 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

Optional query parameters of type boolean or string array, e.g.

parameters:
  - name: myFlag
    required: false
    in: query
    schema:
      type: boolean
  - name: myStrings
    required: false
    in: query
    schema:
      type: array
      items:
        type: string

are not being generated properly via the templates, causing a 500 error
if they are not provided in the HTTP call.

openapi-generator version

5.1.1
I don't believe this is a regression.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: 'OpenAPI Petstore'
  title: OpenAPI Petstore
  version: 1.0.0
servers:
- description: The local server
  url: https://localhost:8080/{version}
paths:
  /pets:
    get:
      operationId: getPet
      parameters:
      - description: Only return pets with these tag names
        in: query
        name: tagNames
        required: false
        schema:
          type: array
          items:
            type: string
      - description: Only return pets with available status
        in: query
        name: isAvailable
        required: false
        schema:
          type: boolean
      responses:
        "200":
          content: {}
          description: ""
Generation Details
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:latest generate \
-i /local/openapi.yaml \
-g go-server -o /local/go-server
Steps to reproduce

Generate the code and look in the routers.go file for parseBoolParameter which does
not have a required flag.
Also notice there is no parseStringArrayParameter function, so api_pets.go will do a strings.Split on tagNames.
If tagNames is not provided, it will be set to [""] which is incorrect.

Related issues/PRs
Suggest a fix

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

Generate the go-server output with the provided OpenAPI declaration and inspect routers.go and api_pets.go. Start at parseBoolParameter and the string-array handling described in the issue. Done means omitted optional boolean and string-array query parameters no longer cause a 500 or produce an array containing an empty string.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.