You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[go] wish to optionally strict json.Unmarshall in decode()
currently when generating go code json.Unmarshall is used to map json objects into go structures.
however by default json.Unmarshall does not throw an error when a json key has no matching go strcture to map on.
As stated on
By default, object keys which don't have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative).
example generated code for decode (6.0.0-SNAPSHOT):
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
if len(b) == 0 {
return nil
}
if s, ok := v.(*string); ok {
*s = string(b)
return nil
}
if f, ok := v.(**os.File); ok {
*f, err = ioutil.TempFile("", "HttpClientFile")
if err != nil {
return
}
_, err = (*f).Write(b)
if err != nil {
return
}
_, err = (*f).Seek(0, io.SeekStart)
return
}
if xmlCheck.MatchString(contentType) {
if err = xml.Unmarshal(b, v); err != nil {
return err
}
return nil
}
if jsonCheck.MatchString(contentType) {
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
return err
}
} else {
return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
}
} else if err = json.Unmarshal(b, v); err != nil { // simple model
return err
}
return nil
}
return errors.New("undefined response type")
}
In case of //simple model, reading(decoding) json structures might ignore
objects/keys are not decoded (in case of schema mismatch)
Wish to optionally generate (json) decoder with strict schema checking
I would prefer to have an early warning option to catch the case of schema/data mismatch when
reading/decoding json data, to make sure we are aware (with error)
objects/keys are going to be lost.
Currently i notice a "newstrictDecoder()" in client.go but it looks like
not be-ing used for this purpose.
I was expecting(hoping) the option "--strict-spec true" would trigger a feature
also to check the actual data to match on schema but it is "only" checking the specfile, not actual data
as it seems. --strict-spec Feature described in #1086
Current "shortcut"
Currently put in as a hack client.go:decode to perform such check:
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
if len(b) == 0 {
return nil
}
if s, ok := v.(*string); ok {
*s = string(b)
return nil
}
if f, ok := v.(**os.File); ok {
*f, err = ioutil.TempFile("", "HttpClientFile")
if err != nil {
return
}
_, err = (*f).Write(b)
if err != nil {
return
}
_, err = (*f).Seek(0, io.SeekStart)
return
}
if xmlCheck.MatchString(contentType) {
if err = xml.Unmarshal(b, v); err != nil {
return err
}
return nil
}
if jsonCheck.MatchString(contentType) {
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
return err
}
} else {
return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
}
} else if err = safeJSONUnmarshalbyte(b, v); err != nil { // Hack ArFi to implement strict json map checks
// err = json.Unmarshal(b, v); err != nil { // simple model
return err
}
return nil
}
return errors.New("undefined response type")
}
The text was updated successfully, but these errors were encountered:
[go] wish to optionally strict json.Unmarshall in decode()
currently when generating go code json.Unmarshall is used to map json objects into go structures.
however by default json.Unmarshall does not throw an error when a json key has no matching go strcture to map on.
As stated on
json#Unmarshal:
example generated code for decode (6.0.0-SNAPSHOT):
In case of //simple model, reading(decoding) json structures might ignore
objects/keys are not decoded (in case of schema mismatch)
Wish to optionally generate (json) decoder with strict schema checking
I would prefer to have an early warning option to catch the case of schema/data mismatch when
reading/decoding json data, to make sure we are aware (with error)
objects/keys are going to be lost.
Currently i notice a "newstrictDecoder()" in client.go but it looks like
not be-ing used for this purpose.
I was expecting(hoping) the option "--strict-spec true" would trigger a feature
also to check the actual data to match on schema but it is "only" checking the specfile, not actual data
as it seems.
--strict-spec Feature described in #1086
Current "shortcut"
Currently put in as a hack client.go:decode to perform such check:
The text was updated successfully, but these errors were encountered: