Skip to content

Commit be05b6b

Browse files
committed
fix: differentiate between array typres and standard structs
1 parent 11ee6f7 commit be05b6b

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

openapi.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,17 @@ func RegisterOpenAPIOperation[T, B any](s *Server, method, path string) (*openap
164164
bodyTag := tagFromType(*new(B))
165165
if (method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch) && bodyTag != "unknown-interface" && bodyTag != "string" {
166166

167-
bodySchema, ok := s.OpenApiSpec.Components.Schemas[bodyTag]
168-
if !ok {
169-
var err error
170-
bodySchema, err = generator.NewSchemaRefForValue(new(B), s.OpenApiSpec.Components.Schemas)
171-
if err != nil {
172-
return operation, err
173-
}
174-
s.OpenApiSpec.Components.Schemas[bodyTag] = bodySchema
167+
bodySchema, err := generator.NewSchemaRefForValue(new(B), s.OpenApiSpec.Components.Schemas)
168+
if err != nil {
169+
return operation, err
175170
}
176171

172+
content := openapi3.NewContentWithSchema(bodySchema.Value, []string{"application/json"})
173+
content["application/json"].Schema.Ref = "#/components/schemas/" + bodyTag
177174
requestBody := openapi3.NewRequestBody().
178175
WithRequired(true).
179-
WithDescription("Request body for " + reflect.TypeOf(*new(B)).String())
180-
181-
if bodySchema != nil {
182-
content := openapi3.NewContentWithSchema(bodySchema.Value, []string{"application/json"})
183-
content["application/json"].Schema.Ref = "#/components/schemas/" + bodyTag
184-
requestBody.WithContent(content)
185-
}
176+
WithDescription("Request body for " + reflect.TypeOf(*new(B)).String()).
177+
WithContent(content)
186178

187179
s.OpenApiSpec.Components.RequestBodies[bodyTag] = &openapi3.RequestBodyRef{
188180
Value: requestBody,
@@ -196,23 +188,17 @@ func RegisterOpenAPIOperation[T, B any](s *Server, method, path string) (*openap
196188
}
197189

198190
tag := tagFromType(*new(T))
199-
// Response body
200-
responseSchema, ok := s.OpenApiSpec.Components.Schemas[tag]
201-
if !ok {
202-
var err error
203-
responseSchema, err = generator.NewSchemaRefForValue(new(T), s.OpenApiSpec.Components.Schemas)
204-
if err != nil {
205-
return operation, err
206-
}
207-
s.OpenApiSpec.Components.Schemas[tag] = responseSchema
191+
responseSchema, err := generator.NewSchemaRefForValue(new(T), s.OpenApiSpec.Components.Schemas)
192+
if err != nil {
193+
return operation, err
208194
}
209195

210-
response := openapi3.NewResponse().WithDescription("OK")
211-
if responseSchema != nil {
212-
content := openapi3.NewContentWithSchema(responseSchema.Value, []string{"application/json"})
213-
content["application/json"].Schema.Ref = "#/components/schemas/" + tag
214-
response.WithContent(content)
215-
}
196+
content := openapi3.NewContentWithSchema(responseSchema.Value, []string{"application/json"})
197+
content["application/json"].Schema.Ref = "#/components/schemas/" + tag
198+
response := openapi3.NewResponse().
199+
WithDescription("OK").
200+
WithContent(content)
201+
216202
operation.AddResponse(200, response)
217203

218204
// Path parameters

openapi_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestServer_generateOpenAPI(t *testing.T) {
6767
require.NotNil(t, document.Paths.Find("/"))
6868
require.Nil(t, document.Paths.Find("/unknown"))
6969
require.NotNil(t, document.Paths.Find("/post"))
70+
require.Equal(t, document.Paths.Find("/post").Post.Responses.Value("200").Value.Content["application/json"].Schema.Value.Type, "array")
7071
require.NotNil(t, document.Paths.Find("/post/{id}").Get.Responses.Value("200"))
7172
require.NotNil(t, document.Paths.Find("/post/{id}").Get.Responses.Value("200").Value.Content["application/json"])
7273
require.Nil(t, document.Paths.Find("/post/{id}").Get.Responses.Value("200").Value.Content["application/json"].Schema.Value.Properties["unknown"])

0 commit comments

Comments
 (0)