Skip to content

[BUG] [Golang] JSON unmarshal fails on additional properties when additionalProperties is unset #21121

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

Open
4 of 6 tasks
NautiluX opened this issue Apr 22, 2025 · 2 comments
Open
4 of 6 tasks

Comments

@NautiluX
Copy link

NautiluX commented Apr 22, 2025

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

When a model lists required fields, but additionalProperties is not set, it should treat it as being set to true, but instead the JSON parsing fails with an error like.

json: unknown field "moreStuff"
openapi-generator version

7.12.0

OpenAPI declaration file content or url

Minimal OpenAPI spec to reproduce (spec.yaml):

openapi: 3.0.0
info:
  title: Sample API
  description: Minimal spec
  version: 0.0.1

servers:
  - url: http://localhost:8080

paths:
  /something:
    get:
      summary: Returns something
      responses:
        "200":
          description: A JSON array of somethings
          content:
            application/json:
              schema:
                type: object
                properties:
                  stuff:
                    type: string
                required:
                - stuff
Generation Details
openapi-generator-cli generate -g go -i spec.yaml
Steps to reproduce

Run web server serving problematic (but not incompatible with the spec!) JSON, like the following (server.go):

package main

import (
	"encoding/json"
	"net/http"
)

type Response struct {
	Stuff     string `json:"stuff"`
	MoreStuff string `json:"moreStuff"`
}

func handler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	response := Response{Stuff: "something", MoreStuff: "something else"}
	json.NewEncoder(w).Encode(response)
}

func main() {
	http.HandleFunc("/something", handler)
	http.ListenAndServe(":8080", nil)
}

using a command like

go run server.go

After generating the client, remove the skip in line ~27 in the test file test/api_default_test.go,
onstall deps and run the test

go mod tidy
go test ./...
Error:      	Expected nil, but got: &openapi.GenericOpenAPIError{[...], error:"json: unknown field \"moreStuff\"", model:interface {}(nil)}

Adding additionalProperties: true to the spec like following makes the test pass:

openapi: 3.0.0
info:
  title: Sample API
  description: Minimal spec
  version: 0.0.1

servers:
  - url: http://localhost:8080

paths:
  /something:
    get:
      summary: Returns something
      responses:
        "200":
          description: A JSON array of somethings
          content:
            application/json:
              schema:
                type: object
                additionalProperties: true
                properties:
                  stuff:
                    type: string
                required:
                - stuff
Related issues/PRs

#17267 added the validation of unknown properties when required fields are set but additionalProperties is not.

Suggest a fix

I think the problem is that an empty additionalProperties field is not being treated equal to being set to true. As far as I understand that's how it should be handled, though.

@ctreatma
Copy link
Contributor

ctreatma commented May 2, 2025

By default, this generator generates closed schemas. It does that for historical reasons, and I think it would be good to change the default at this point.

Until the default configuration is changed, you will need to update your generator config to set disallowAdditionalPropertiesIfNotSet=false. With that setting, the generator will treat an empty additionalProperties as being equal to true.

@NautiluX
Copy link
Author

NautiluX commented May 8, 2025

Thanks, I'll give it a try!

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

2 participants