Skip to content

Commit

Permalink
Use variadic options
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Nov 3, 2023
1 parent 1ecf491 commit a3a0221
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
14 changes: 4 additions & 10 deletions internal/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func ReflectRequestBody(
// ReflectJSONResponse reflects JSON schema of response.
func ReflectJSONResponse(
r *jsonschema.Reflector,
reflOption func(rc *jsonschema.ReflectContext),
output interface{},
reflOptions ...func(rc *jsonschema.ReflectContext),
) (schema *jsonschema.Schema, err error) {
if output == nil {
return nil, nil
Expand All @@ -154,18 +154,12 @@ func ReflectJSONResponse(
return nil, nil
}

if reflOption == nil {
reflOption = func(rc *jsonschema.ReflectContext) {}
}

sch, err := r.Reflect(output,
reflOption,
// openapi.WithOperationCtx(oc, true, openapi.InBody),
reflOptions = append(reflOptions,
jsonschema.RootRef,
// jsonschema.DefinitionsPrefix(componentsSchemas),
// jsonschema.CollectDefinitions(r.collectDefinition()),
sanitizeDefName,
)

sch, err := r.Reflect(output, reflOptions...)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,13 @@ func (r *Reflector) ensureResponseContentType(resp *Response, contentType string
}

func (r *Reflector) parseJSONResponse(resp *Response, oc openapi.OperationContext, cu openapi.ContentUnit) error {
sch, err := internal.ReflectJSONResponse(r.JSONSchemaReflector(), func(rc *jsonschema.ReflectContext) {
openapi.WithOperationCtx(oc, true, openapi.InBody)(rc)
jsonschema.DefinitionsPrefix(componentsSchemas)(rc)
jsonschema.CollectDefinitions(r.collectDefinition())(rc)
}, cu.Structure)
sch, err := internal.ReflectJSONResponse(
r.JSONSchemaReflector(),
cu.Structure,
openapi.WithOperationCtx(oc, true, openapi.InBody),
jsonschema.DefinitionsPrefix(componentsSchemas),
jsonschema.CollectDefinitions(r.collectDefinition()),
)

if err != nil || sch == nil {
return err
Expand Down
8 changes: 3 additions & 5 deletions openapi31/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,10 @@ func (r *Reflector) ensureResponseContentType(resp *Response, contentType string
func (r *Reflector) parseJSONResponse(resp *Response, oc openapi.OperationContext, cu openapi.ContentUnit) error {
sch, err := internal.ReflectJSONResponse(
r.JSONSchemaReflector(),
func(rc *jsonschema.ReflectContext) {
openapi.WithOperationCtx(oc, true, openapi.InBody)(rc)
jsonschema.DefinitionsPrefix(componentsSchemas)(rc)
jsonschema.CollectDefinitions(r.collectDefinition())(rc)
},
cu.Structure,
openapi.WithOperationCtx(oc, true, openapi.InBody),
jsonschema.DefinitionsPrefix(componentsSchemas),
jsonschema.CollectDefinitions(r.collectDefinition()),
)

if err != nil || sch == nil {
Expand Down

0 comments on commit a3a0221

Please sign in to comment.