Skip to content

Commit

Permalink
[OpenAPI] Fix 204 specification & add reference to error model schema
Browse files Browse the repository at this point in the history
  • Loading branch information
danysousa committed Dec 19, 2023
1 parent 912c9a0 commit 5bdb617
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions openapi/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (b *DocBuilder) WithResponse(output interface{}, options ...BuilderOption)

if output == nil {
c.statusCode = 204
c.mimeType = ""
}

c.applyOptions(options...)
Expand All @@ -180,7 +181,7 @@ func (b *DocBuilder) WithResponse(output interface{}, options ...BuilderOption)
resp.ResponseEns().WithDescription(c.description)
}

if len(c.examples) == 0 {
if len(c.examples) == 0 && output != nil {
c.examples = make(map[string]openapi3.Example)
exampleName := "default"
c.examples[exampleName] = openapi3.Example{
Expand All @@ -190,7 +191,7 @@ func (b *DocBuilder) WithResponse(output interface{}, options ...BuilderOption)
}
}

if len(c.examples) != 0 {
if len(c.examples) != 0 && output != nil {
contentResp := resp.ResponseEns().Content[c.mimeType]

for key, item := range c.examples {
Expand All @@ -212,16 +213,26 @@ func (b *DocBuilder) WithResponse(output interface{}, options ...BuilderOption)
// - WithDescription to add a description to error response
func (b *DocBuilder) WithError(statusCode int, kind, message string, options ...BuilderOption) *DocBuilder {
c := builderOptions{
examples: nil,
statusCode: statusCode,
description: message,
mimeType: "application/json",
}

c.applyOptions(options...)

exampleValue := errors.Err(kind, message).WithStatus(statusCode)

if err := b.reflector.SetJSONResponse(b.operation, exampleValue, statusCode); err != nil {
exampleValue := errors.HttpError{
Message: message,
Kind: kind,
}
err := b.reflector.SetupResponse(openapi3.OperationContext{
Operation: b.operation,
Output: exampleValue,
HTTPStatus: c.statusCode,
RespContentType: c.mimeType,
RespHeaderMapping: c.headers,
})
if err != nil {
b.err = append(b.err, err)
}

Expand Down

0 comments on commit 5bdb617

Please sign in to comment.