Skip to content

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

Open
@NautiluX

Description

@NautiluX

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions