Skip to content

Commit

Permalink
openapi{2,3}: surface both json/yaml unmarshal errors (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
timothympace committed Jun 2, 2024
1 parent 03281ec commit 2cf1262
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions openapi2/marsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ func unmarshalError(jsonUnmarshalErr error) error {
}

func unmarshal(data []byte, v interface{}) error {
var jsonErr, yamlErr error

// See https://github.com/getkin/kin-openapi/issues/680
if err := json.Unmarshal(data, v); err != nil {
// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys
return yaml.Unmarshal(data, v)
if jsonErr = json.Unmarshal(data, v); jsonErr == nil {
return nil
}

// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys
if yamlErr = yaml.Unmarshal(data, v); yamlErr == nil {
return nil
}
return nil

// If both unmarshaling attempts fail, return a new error that includes both errors
return fmt.Errorf("failed to unmarshal data: json error: %v, yaml error: %v", jsonErr, yamlErr)
}
16 changes: 12 additions & 4 deletions openapi3/marsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ func unmarshalError(jsonUnmarshalErr error) error {
}

func unmarshal(data []byte, v interface{}) error {
var jsonErr, yamlErr error

// See https://github.com/getkin/kin-openapi/issues/680
if err := json.Unmarshal(data, v); err != nil {
// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys
return yaml.Unmarshal(data, v)
if jsonErr = json.Unmarshal(data, v); jsonErr == nil {
return nil
}

// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys
if yamlErr = yaml.Unmarshal(data, v); yamlErr == nil {
return nil
}
return nil

// If both unmarshaling attempts fail, return a new error that includes both errors
return fmt.Errorf("failed to unmarshal data: json error: %v, yaml error: %v", jsonErr, yamlErr)
}

0 comments on commit 2cf1262

Please sign in to comment.