Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions datamodel/spec_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,22 @@
_, openAPI2 := utils.FindKeyNode(utils.OpenApi2, parsedSpec.Content)
_, asyncAPI := utils.FindKeyNode(utils.AsyncApi, parsedSpec.Content)

parseJSON := func(bytes []byte, spec *SpecInfo, parsedNode *yaml.Node) {
parseJSON := func(bytes []byte, spec *SpecInfo, parsedNode *yaml.Node) error {
var jsonSpec map[string]interface{}
if utils.IsYAML(string(bytes)) {
switch {
case utils.IsYAML(string(bytes)):
_ = parsedNode.Decode(&jsonSpec)
b, _ := json.Marshal(&jsonSpec)
spec.SpecJSONBytes = &b
spec.SpecJSON = &jsonSpec
} else {
_ = json.Unmarshal(bytes, &jsonSpec)
case utils.IsJSON(string(bytes)):
if err := json.Unmarshal(bytes, &jsonSpec); err != nil {
return err
}
spec.SpecJSONBytes = &bytes
spec.SpecJSON = &jsonSpec
}
return nil
}

// if !bypass {
Expand Down Expand Up @@ -149,7 +153,10 @@
}

// parse JSON
parseJSON(spec, specInfo, &parsedSpec)
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
specInfo.Error = fmt.Errorf("unable to parse specification: %w", err)
return specInfo, specInfo.Error
}
parsed = true

// double check for the right version, people mix this up.
Expand All @@ -176,7 +183,10 @@
specInfo.APISchema = OpenAPI2SchemaData

// parse JSON
parseJSON(spec, specInfo, &parsedSpec)
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
specInfo.Error = fmt.Errorf("unable to parse specification: %w", err)
return specInfo, specInfo.Error
}

Check warning on line 189 in datamodel/spec_info.go

View check run for this annotation

Codecov / codecov/patch

datamodel/spec_info.go#L187-L189

Added lines #L187 - L189 were not covered by tests
parsed = true

// I am not certain this edge-case is very frequent, but let's make sure we handle it anyway.
Expand All @@ -200,7 +210,10 @@
// TODO: format for AsyncAPI.

// parse JSON
parseJSON(spec, specInfo, &parsedSpec)
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
specInfo.Error = fmt.Errorf("unable to parse specification: %w", err)
return specInfo, nil
}

Check warning on line 216 in datamodel/spec_info.go

View check run for this annotation

Codecov / codecov/patch

datamodel/spec_info.go#L214-L216

Added lines #L214 - L216 were not covered by tests
parsed = true

// so far there is only 2 as a major release of AsyncAPI
Expand All @@ -215,7 +228,10 @@
if specInfo.SpecType == "" {
// parse JSON
if !bypass {
parseJSON(spec, specInfo, &parsedSpec)
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
specInfo.Error = errors.New("spec type not supported by libopenapi, sorry")
return specInfo, specInfo.Error
}

Check warning on line 234 in datamodel/spec_info.go

View check run for this annotation

Codecov / codecov/patch

datamodel/spec_info.go#L232-L234

Added lines #L232 - L234 were not covered by tests
parsed = true
specInfo.Error = errors.New("spec type not supported by libopenapi, sorry")
return specInfo, specInfo.Error
Expand All @@ -227,7 +243,10 @@
//}

if !parsed {
parseJSON(spec, specInfo, &parsedSpec)
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
specInfo.Error = fmt.Errorf("unable to parse specification: %w", err)
return specInfo, specInfo.Error
}

Check warning on line 249 in datamodel/spec_info.go

View check run for this annotation

Codecov / codecov/patch

datamodel/spec_info.go#L247-L249

Added lines #L247 - L249 were not covered by tests
}

// detect the original whitespace indentation
Expand Down
12 changes: 9 additions & 3 deletions datamodel/spec_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const (
)

var (
goodJSON = `{"name":"kitty", "noises":["meow","purrrr","gggrrraaaaaooooww"]}`
badJSON = `{"name":"kitty, "noises":[{"meow","purrrr","gggrrraaaaaooooww"]}}`
goodYAML = `name: kitty
goodJSON = `{"name":"kitty", "noises":["meow","purrrr","gggrrraaaaaooooww"]}`
badJSON = `{"name":"kitty, "noises":[{"meow","purrrr","gggrrraaaaaooooww"]}}`
badJSONContainingComma = `{"openapi":"3.0.3","info":{"title":"Broken API Spec","version":"1.0.0"},"paths":{"/ping":{"get":{"summary":"Ping endpoint","responses":{"200":{"description":"OK",}}}}}}`
goodYAML = `name: kitty
noises:
- meow
- purrr
Expand Down Expand Up @@ -118,6 +119,11 @@ func TestExtractSpecInfo_InvalidJSON(t *testing.T) {
assert.Error(t, e)
}

func TestExtractSpecInfo_InvalidJSONContainingExtraComma(t *testing.T) {
_, e := ExtractSpecInfo([]byte(badJSONContainingComma))
assert.EqualError(t, e, "unable to parse specification: invalid character '}' looking for beginning of object key string")
}

func TestExtractSpecInfo_Nothing(t *testing.T) {
_, e := ExtractSpecInfo([]byte(""))
assert.Error(t, e)
Expand Down
12 changes: 6 additions & 6 deletions document_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ func TestExampleCompareDocuments_swagger(t *testing.T) {
func TestDocument_Paths_As_Array(t *testing.T) {
// paths can now be wrapped in an array.
spec := `{
"openapi": "3.1.0",
"paths": [
"/": {
"get": {}
}
]
"openapi": "3.1.0",
"paths": {
"/": {
"get": {}
}
}
}
`
// create a new document from specification bytes
Expand Down
6 changes: 3 additions & 3 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,9 @@ func TestDocument_BuildModelBad(t *testing.T) {
}

func TestDocument_Serialize_JSON_Modified(t *testing.T) {
json := `{ 'openapi': '3.0',
'info': {
'title': 'The magic API'
json := `{ "openapi": "3.0",
"info": {
"title": "The magic API"
}
}
`
Expand Down