-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Closed
Labels
Description
Description
I have a use case when I need to read a schema object from a file. If the object contains unknown fields, I need to get an error to make sure I am not ignoring the data (e.g. when reader code uses outdated schema).
The following code used to work prior to 7.1.0:
var obj SomeObject
decoder := json.NewDecoder(bytes.NewBuffer([]byte(jsonString)))
decoder.DisallowUnknownFields()
err := decoder.Decode(&obj)openapi-generator version
- 7.1.0
- latest
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Sample API
version: 0.1.9
paths:
/test:
get:
responses:
'200':
description: Some description
content:
application/json:
schema:
$ref: '#/components/schemas/SomeObject'
components:
schemas:
SomeObject:
additionalProperties: false
required:
- field1
properties:
field1:
type: string
field2:
type: string
Generation Details
docker run --rm -u 1001:1001 -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/test.yaml -g go -o /local/out/go
Steps to reproduce
package openapi
import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
func TestColdStart(t *testing.T) {
jsonString := `
{
"field1": "a",
"field2": "b",
"field3": "c"
}
`
var obj SomeObject
decoder := json.NewDecoder(bytes.NewBuffer([]byte(jsonString)))
decoder.DisallowUnknownFields()
err := decoder.Decode(&obj)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", obj)
t.Fail()
}Expected behavior:
- get an error like
json: unknown field "field3"
Related issues/PRs
- [Go] validate required fields when unmarshalling JSON #16863 that added generation of
UnmarshalJSON()method
Reactions are currently unavailable