OpenAPITools / OpenAPITools/openapi-generator

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

Open
#21,121 2 comments 1 reaction 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

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.

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

Reproduce the issue using spec.yaml, the generated Go client, and test/api_default_test.go after removing the skip; start by reviewing the unknown-property validation introduced by related issue #17267. Done means the generated client accepts extra JSON properties when additionalProperties is omitted, while preserving validation for required fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.