OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.97k stars 6.59k forks source link

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

Open chenaoxd opened 4 days ago

chenaoxd commented 4 days ago

Bug Report Checklist

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.

chenaoxd commented 4 days ago

And in the openapi: 3.0.3 version, it treats both setting equaivalently.