Skip to content

Commit d842773

Browse files
committed
chore: Fix apiSpecDoc content model.
1 parent 1e51d55 commit d842773

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

internal/dto/apiSpecDoc/apiSpecDoc.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,30 @@ func (sc *Schema) FindField(name string) *Schema {
166166
// MediaTypeObject represents schema for the different media types
167167
// i.e. "application/json" and etc.
168168
type MediaTypeObject struct {
169-
Schema *Schema
169+
MediaType string
170+
Schema *Schema
170171
}
171172

172173
// RequestBody is a representation of request body
173174
type RequestBody struct {
174175
Description string
175176

176177
//Content represents request object for the different media types
177-
Content map[string]*MediaTypeObject
178+
Content []*MediaTypeObject
178179

179180
//Required define is request body required
180181
Required bool
181182
}
182183

184+
func (rb *RequestBody) FindContentByMediaType(mediaType string) *MediaTypeObject {
185+
for _, mediaTypeObj := range rb.Content {
186+
if mediaTypeObj != nil && mediaTypeObj.MediaType == mediaType {
187+
return mediaTypeObj
188+
}
189+
}
190+
return nil
191+
}
192+
183193
// Server represents server description
184194
// To use in the address line of view
185195
// https://swagger.io/specification/#server-object

internal/parse/openapi/openapi.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,13 @@ func convertBody(body *openapi3.RequestBody) *apiSpecDoc.RequestBody {
139139
specBody := new(apiSpecDoc.RequestBody)
140140
specBody.Description = body.Description
141141
specBody.Required = body.Required
142-
specContent := make(map[string]*apiSpecDoc.MediaTypeObject)
142+
specContent := make([]*apiSpecDoc.MediaTypeObject, 0, len(body.Content))
143143
for cType, content := range body.Content {
144144
if content.Schema == nil || content.Schema.Value == nil {
145145
continue
146146
}
147-
specContent[cType] = &apiSpecDoc.MediaTypeObject{Schema: convertSchema("", content.Schema.Value)}
147+
specContent = append(specContent,
148+
&apiSpecDoc.MediaTypeObject{MediaType: cType, Schema: convertSchema("", content.Schema.Value)})
148149
}
149150
specBody.Content = specContent
150151
return specBody

internal/parse/openapi/openapi_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func TestOpenapiToApiSpec(t *testing.T) {
8989
pmBody := postM.RequestBody
9090
assert.NotNil(t, pmBody.Content)
9191
assert.True(t, pmBody.Required)
92-
pmContent, ok := pmBody.Content["application/json"]
93-
assert.True(t, ok)
92+
pmContent := pmBody.FindContentByMediaType("application/json")
93+
assert.NotNil(t, pmContent)
9494
assert.NotNil(t, pmContent.Schema)
9595
pmSchema := pmContent.Schema
9696
assert.Equal(t, apiSpecDoc.Object, pmSchema.Type)
@@ -139,8 +139,8 @@ func TestOpenapiToApiSpec(t *testing.T) {
139139
gBody := postG.RequestBody
140140
assert.NotNil(t, gBody.Content)
141141
assert.True(t, gBody.Required)
142-
gContent, ok := gBody.Content["application/json"]
143-
assert.True(t, ok)
142+
gContent := gBody.FindContentByMediaType("application/json")
143+
assert.NotNil(t, gContent)
144144
assert.NotNil(t, gContent.Schema)
145145
gSchema := gContent.Schema
146146
assert.Equal(t, apiSpecDoc.Object, gSchema.Type)

0 commit comments

Comments
 (0)