From 6e51aee86b2788d30807b3696fc5d255dce8eac9 Mon Sep 17 00:00:00 2001 From: "julien.girard" Date: Tue, 4 Nov 2025 14:28:18 +0100 Subject: [PATCH] feat: Add the possibility to set examples to the default handler behavior When trying to add examples for the default tonic status with the function fizz.Response, we get the following error: panic: error while generating OpenAPI spec on operation XXXX : response with code XXX already exists [recovered, repanicked] This commit allow to define examples for the default handler behavior Signed-off-by: julien.girard --- fizz.go | 7 +++++++ openapi/generator.go | 4 ++-- openapi/operation.go | 25 +++++++++++++------------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/fizz.go b/fizz.go index 7339898..36a6a88 100644 --- a/fizz.go +++ b/fizz.go @@ -346,6 +346,13 @@ func ResponseWithExamples(statusCode, desc string, model interface{}, headers [] } } +// DefaultResponseExamples adds additional examples to the default response. +func DefaultResponseExamples(examples map[string]interface{}) func(*openapi.OperationInfo) { + return func(o *openapi.OperationInfo) { + o.DefaultResponseExamples = examples + } +} + // Header adds a header to the operation. func Header(name, desc string, model interface{}) func(*openapi.OperationInfo) { return func(o *openapi.OperationInfo) { diff --git a/openapi/generator.go b/openapi/generator.go index 99f6833..78fa988 100644 --- a/openapi/generator.go +++ b/openapi/generator.go @@ -289,7 +289,7 @@ func (g *Generator) AddOperation(path, method, tag string, in, out reflect.Type, // Generate the default response from the tonic // handler return type. If the handler has no output // type, the response won't have a schema. - if err := g.setOperationResponse(op, out, strconv.Itoa(info.StatusCode), tonic.MediaType(), info.StatusDescription, info.Headers, nil, nil); err != nil { + if err := g.setOperationResponse(op, out, strconv.Itoa(info.StatusCode), tonic.MediaType(), info.StatusDescription, info.Headers, nil, info.DefaultResponseExamples); err != nil { return nil, err } // Generate additional responses from the operation @@ -1255,7 +1255,7 @@ func fieldNameFromTag(sf reflect.StructField, tagName string) string { return name } -/// parseExampleValue is used to transform the string representation of the example value to the correct type. +// parseExampleValue is used to transform the string representation of the example value to the correct type. func parseExampleValue(t reflect.Type, value string) (interface{}, error) { // If the type implements Exampler use the ParseExample method to create the example i, ok := reflect.New(t).Interface().(Exampler) diff --git a/openapi/operation.go b/openapi/operation.go index 306900f..fb8f5b3 100644 --- a/openapi/operation.go +++ b/openapi/operation.go @@ -3,18 +3,19 @@ package openapi // OperationInfo represents the informations of an operation // that will be used when generating the OpenAPI specification. type OperationInfo struct { - ID string - StatusCode int - StatusDescription string - Headers []*ResponseHeader - Summary string - Description string - Deprecated bool - InputModel interface{} - Responses []*OperationResponse - Security []*SecurityRequirement - XCodeSamples []*XCodeSample - XInternal bool + ID string + StatusCode int + StatusDescription string + Headers []*ResponseHeader + Summary string + Description string + Deprecated bool + InputModel interface{} + Responses []*OperationResponse + Security []*SecurityRequirement + XCodeSamples []*XCodeSample + XInternal bool + DefaultResponseExamples map[string]interface{} } // ResponseHeader represents a single header that