Skip to content

Commit

Permalink
refactor: ignonre err from generator.NewSchemaRefForValue
Browse files Browse the repository at this point in the history
Ignore this error as it is not possible with the current
instationation of of the openapi generator used. Unless
cyclic err is set or a custom schema customizer is provided
it is not possible to error.
  • Loading branch information
dylanhitt committed Jul 1, 2024
1 parent 123a898 commit 58a8cc9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
41 changes: 13 additions & 28 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ func RegisterOpenAPIOperation[T, B any](s *Server, method, path string) (*openap
}

// Request body
bodyTag, err := schemaTagFromType[B](s, *new(B))
if err != nil {
return operation, err
}
bodyTag := schemaTagFromType[B](s, *new(B))
if (method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch) && bodyTag.name != "unknown-interface" && bodyTag.name != "string" {
content := openapi3.NewContentWithSchemaRef(&bodyTag.SchemaRef, []string{"application/json"})
requestBody := openapi3.NewRequestBody().
Expand All @@ -183,10 +180,7 @@ func RegisterOpenAPIOperation[T, B any](s *Server, method, path string) (*openap
}
}

responseSchema, err := schemaTagFromType[T](s, *new(T))
if err != nil {
return operation, err
}
responseSchema := schemaTagFromType[T](s, *new(T))
content := openapi3.NewContentWithSchemaRef(&responseSchema.SchemaRef, []string{"application/json"})
response := openapi3.NewResponse().
WithDescription("OK").
Expand All @@ -211,64 +205,55 @@ type schemaTag struct {
name string
}

func schemaTagFromType[V any](s *Server, v any) (schemaTag, error) {
func schemaTagFromType[V any](s *Server, v any) schemaTag {
if v == nil {
return schemaTag{
name: "unknown-interface",
SchemaRef: openapi3.SchemaRef{
Ref: "#/components/schemas/unknown-interface",
},
}, nil
}
}

return dive[V](s, reflect.TypeOf(v), schemaTag{}, 5)
}

func dive[V any](s *Server, t reflect.Type, tag schemaTag, maxDepth int) (schemaTag, error) {
func dive[V any](s *Server, t reflect.Type, tag schemaTag, maxDepth int) schemaTag {
if maxDepth == 0 {
return schemaTag{
name: "default",
SchemaRef: openapi3.SchemaRef{
Ref: "#/components/schemas/default",
},
}, nil
}
}

switch t.Kind() {
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Func, reflect.UnsafePointer:
return dive[V](s, t.Elem(), tag, maxDepth-1)

case reflect.Slice, reflect.Array:
item, err := dive[V](s, t.Elem(), tag, maxDepth-1)
if err != nil {
return schemaTag{}, err
}

item := dive[V](s, t.Elem(), tag, maxDepth-1)
tag.name = item.name
tag.Value = &openapi3.Schema{
Type: "array",
Items: &item.SchemaRef,
}
return tag, nil
return tag

default:
var err error
tag.name = t.Name()
tag.Ref = "#/components/schemas/" + tag.name
tag.Value, err = s.getOrCreateSchema(tag.name, new(V))
return tag, err
tag.Value = s.getOrCreateSchema(tag.name, new(V))
return tag
}
}

func (s *Server) getOrCreateSchema(key string, v any) (*openapi3.Schema, error) {
var err error
func (s *Server) getOrCreateSchema(key string, v any) *openapi3.Schema {
schemaRef, ok := s.OpenApiSpec.Components.Schemas[key]
if !ok {
schemaRef, err = generator.NewSchemaRefForValue(v, s.OpenApiSpec.Components.Schemas)
if err != nil {
return nil, err
}
schemaRef, _ = generator.NewSchemaRefForValue(v, s.OpenApiSpec.Components.Schemas)
s.OpenApiSpec.Components.Schemas[key] = schemaRef
}
return schemaRef.Value, err
return schemaRef.Value
}
4 changes: 1 addition & 3 deletions openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ type testCaseForTagType[V any] struct {
s *Server

expectedTagValue string
expectedErr error
}

func runTestCase[V any](tc testCaseForTagType[V]) func(t *testing.T) {
return func(t *testing.T) {
tag, err := schemaTagFromType[V](tc.s, tc.inputType)
require.Equal(t, tc.expectedErr, err, tc.description)
tag := schemaTagFromType[V](tc.s, tc.inputType)
assert.Equal(t, tc.expectedTagValue, tag.name, tc.description)
}
}
Expand Down

0 comments on commit 58a8cc9

Please sign in to comment.