Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
chenaoxd opened this issue Nov 22, 2024 · 1 comment
Open
5 of 6 tasks

Comments

@chenaoxd
Copy link

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.

@chenaoxd
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant