Skip to content

Commit e2ce029

Browse files
committed
enhance: openapi3: update validateMore() to handle missing info object
1 parent 06d44ba commit e2ce029

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

openapi3/read.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,36 +94,38 @@ type ValidationStatus struct {
9494
OpenAPI string
9595
}
9696

97-
/*
98-
status: false
99-
message: |-
100-
expected Object {
101-
title: 'Medium API',
102-
description: 'Articles that matter on social publishing platform'
103-
} to have key version
104-
missing keys: version
105-
context: "#/info"
106-
openapi: 3.0.0
107-
*/
108-
10997
func validateMore(spec *Spec) (ValidationStatus, error) {
110-
version := strings.TrimSpace(spec.Info.Version)
111-
if len(strings.TrimSpace(version)) == 0 {
112-
version = OASVersionDefault
98+
vs := ValidationStatus{}
99+
if spec == nil {
100+
return vs, ErrSpecNotSet
113101
}
114-
vs := ValidationStatus{OpenAPI: version}
115-
if len(version) == 0 {
116-
jdata, err := jsonutil.MarshalSimple(spec.Info, "", " ")
117-
if err != nil {
118-
return vs, err
119-
}
102+
version := ""
103+
if spec.Info == nil {
120104
vs := ValidationStatus{
121105
Context: "#/info",
122-
Message: fmt.Sprintf("expect Object %s to have key version\nmissing keys:version", string(jdata)),
123-
OpenAPI: "3.0.0"}
124-
return vs, fmt.Errorf("E_OPENAPI3_MISSING_KEY [%s]", "info/version")
106+
Message: "expect spec to have info object)",
107+
OpenAPI: spec.OpenAPI}
108+
return vs, fmt.Errorf("E_OPENAPI3_MISSING_KEY [%s]", "info")
109+
} else {
110+
version = strings.TrimSpace(spec.Info.Version)
111+
if len(strings.TrimSpace(version)) == 0 {
112+
version = OASVersionDefault
113+
}
114+
vs.OpenAPI = version
115+
116+
if len(version) == 0 {
117+
jdata, err := jsonutil.MarshalSimple(spec.Info, "", " ")
118+
if err != nil {
119+
return vs, err
120+
}
121+
vs := ValidationStatus{
122+
Context: "#/info",
123+
Message: fmt.Sprintf("expect Object %s to have key version\nmissing keys:version", string(jdata)),
124+
OpenAPI: spec.OpenAPI}
125+
return vs, fmt.Errorf("E_OPENAPI3_MISSING_KEY [%s]", "info/version")
126+
}
127+
vs.Status = true
125128
}
126-
vs.Status = true
127129
return vs, nil
128130
}
129131

0 commit comments

Comments
 (0)