swagger-api / swagger-api/swagger-codegen

[Go] optional boolean properties set to false are omitted when encoded

Open
#7,391 13 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

If an object defines an optional boolean property it does not end up in the JSON if the value is false

Swagger-codegen version

2.2.2

Swagger declaration file content or url
swagger: '2.0'
info:
  description: 'Demonstrate golang codegen bug'
  version: 1.0.0
schemes:
  - http
paths:
  /pet:
    post:
      summary: Add a new pet to the store
      description: ''
      operationId: addPet
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: body
          name: body
          description: Pet object that needs to be added to the store
          required: true
          schema:
            $ref: '#/definitions/Pet'
      responses:
        '405':
          description: Invalid input
definitions:
  Pet:
    title: a Pet
    description: A pet for sale in the pet store
    type: object
    required:
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
        example: doggie
      vaccinated:
        type: boolean
        description: Indicates whether the Pet's vaccinations are up-to-date
Command line used for generation

java -jar target/swagger-codegen-cli.jar generate -i ./mypetstore.yaml -l go -o ./mypetstore-client-go

Steps to reproduce

Include an optional boolean property (like vaccinated in the API above), set to false and encode it. The field will be missing from the JSON.

Related issues/PRs

If the property is marked as required then things work as expected which I guess was a result of https://github.com/swagger-api/swagger-codegen/issues/4665

Suggest a fix/enhancement

Don't use omitempty for boolean properties, even if optional...
from https://golang.org/pkg/encoding/json/

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

..if you do then sending a false for optional properties is not possible.

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 by running the supplied swagger-codegen command with the YAML example and inspect the generated Go model and its JSON encoding. Compare optional and required boolean properties; done means an optional property set to false is present in the encoded JSON, with coverage for the reproduction case.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.