OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Golang] Incorrect type generation for object type schema with additionalProperties

Open
#20,159 1 comment 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

There is an inconsistency in type generation for object schema with additionalProperties. Specifically, when additionalProperties is set to {} (an empty object), it should be treated the same as when additionalProperties is set to true, but currently it is not, leading to incorrect type generation.

Additionally, the swaggo/swag leads to additionalProperties: {} being generated, which results in incorrect output.

openapi-generator version

7.10.0

OpenAPI declaration file content or url

yaml to reproduce the bug

swagger: "2.0"
info:
  title: demo
  description: demo
  version: 0.0.1
paths:
  /pets:
    get:
      produces:
      - application/json
      responses:
        "200":
          description: OK
          schema:
            items:
              $ref: '#/definitions/Pet'
            type: array
      summary: List all Pets
      tags:
      - Application
definitions:
  Pet:
    type: object
    properties:
      config_wrong:
        $ref: '#/definitions/param.ParamsWrong'
      config_correct:
        $ref: '#/definitions/param.ParamsCorrect'
  param.ParamsWrong:
    additionalProperties: {}
    type: object
  param.ParamsCorrect:
    additionalProperties: true
    type: object

and the result is

// Pet struct for Pet
type Pet struct {
	ConfigWrong map[string]map[string]interface{} `json:"config_wrong,omitempty"`
	ConfigCorrect map[string]interface{} `json:"config_correct,omitempty"`
}
Generation Details

openapi-generator generate -g go -o pkg/api -i openapi.yaml --additional-properties=packageName=api,withGoMod=false

Steps to reproduce

openapi-generator generate -g go -o pkg/api -i openapi.yaml --additional-properties=packageName=api,withGoMod=false

Related issues/PRs

None found.

Suggest a fix

A potential fix would be to standardize the handling of additionalProperties: {} and additionalProperties: true to be treated equivalently, as both should allow for an unspecified number of additional properties. This requires a modification in the parser logic to treat empty object {} as a wildcard, similar to true.

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 openapi.yaml and reproduce the output using the documented openapi-generator generate command with the Go generator. Compare the generated Pet fields for additionalProperties: {} and additionalProperties: true; done means both schemas produce the expected equivalent map type, with regression coverage for the reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.