mirror of
https://github.com/Fishwaldo/huma.git
synced 2025-03-15 19:31:27 +00:00
Merge pull request #16 from danielgtaylor/ref-improvements
Ref improvements
This commit is contained in:
commit
3fc9b54ff6
5 changed files with 32 additions and 6 deletions
|
@ -7,6 +7,10 @@ type AutoConfigVar struct {
|
|||
Example string `json:"example,omitempty"`
|
||||
Default interface{} `json:"default,omitempty"`
|
||||
Enum []interface{} `json:"enum,omitempty"`
|
||||
|
||||
// Exclude the value from being sent to the server. This essentially makes
|
||||
// it a value which is only used in param templates.
|
||||
Exclude bool `json:"exclude,omitempty"`
|
||||
}
|
||||
|
||||
// AutoConfig holds an API's automatic configuration settings for the CLI. These
|
||||
|
|
20
openapi.go
20
openapi.go
|
@ -73,9 +73,23 @@ func (c *oaComponents) AddSchema(t reflect.Type, mode schema.Mode, hint string)
|
|||
name = hint
|
||||
}
|
||||
|
||||
s, err := schema.GenerateWithMode(t, mode, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
var s *schema.Schema
|
||||
|
||||
if t.Kind() == reflect.Slice {
|
||||
// We actually want to create two models: one for the container slice
|
||||
// and one for the items within it.
|
||||
ref := c.AddSchema(t.Elem(), mode, name+"Item")
|
||||
s = &schema.Schema{
|
||||
Type: schema.TypeArray,
|
||||
Items: &schema.Schema{
|
||||
Ref: ref,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
if s, err = schema.GenerateWithMode(t, mode, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
orig := name
|
||||
|
|
|
@ -22,6 +22,7 @@ type Operation struct {
|
|||
params map[string]oaParam
|
||||
requestContentType string
|
||||
requestSchema *schema.Schema
|
||||
requestModel reflect.Type
|
||||
responses []Response
|
||||
maxBodyBytes int64
|
||||
bodyReadTimeout time.Duration
|
||||
|
@ -70,7 +71,8 @@ func (o *Operation) toOpenAPI(components *oaComponents) *gabs.Container {
|
|||
if ct == "" {
|
||||
ct = "application/json"
|
||||
}
|
||||
doc.Set(o.requestSchema, "requestBody", "content", ct, "schema")
|
||||
ref := components.AddSchema(o.requestModel, schema.ModeAll, o.id+"-request")
|
||||
doc.Set(ref, "requestBody", "content", ct, "schema", "$ref")
|
||||
}
|
||||
|
||||
// responses
|
||||
|
@ -98,7 +100,7 @@ func (o *Operation) toOpenAPI(components *oaComponents) *gabs.Container {
|
|||
}
|
||||
|
||||
if resp.model != nil {
|
||||
ref := components.AddSchema(resp.model, schema.ModeRead, o.id)
|
||||
ref := components.AddSchema(resp.model, schema.ModeAll, o.id+"-response")
|
||||
doc.Set(ref, "responses", status, "content", resp.contentType, "schema", "$ref")
|
||||
}
|
||||
}
|
||||
|
@ -179,6 +181,7 @@ func (o *Operation) Run(handler interface{}) {
|
|||
|
||||
// Get body if present.
|
||||
if body, ok := input.FieldByName("Body"); ok {
|
||||
o.requestModel = body.Type
|
||||
o.requestSchema, err = schema.GenerateWithMode(body.Type, schema.ModeWrite, nil)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("unable to generate JSON schema: %w", err))
|
||||
|
|
|
@ -73,6 +73,10 @@ func (r *Router) OpenAPI() *gabs.Container {
|
|||
doc.Set(r.description, "info", "description")
|
||||
}
|
||||
|
||||
if len(r.servers) > 0 {
|
||||
doc.Set(r.servers, "servers")
|
||||
}
|
||||
|
||||
components := &oaComponents{
|
||||
Schemas: map[string]*schema.Schema{},
|
||||
SecuritySchemes: r.securitySchemes,
|
||||
|
|
|
@ -144,13 +144,14 @@ type Schema struct {
|
|||
WriteOnly bool `json:"writeOnly,omitempty"`
|
||||
Deprecated bool `json:"deprecated,omitempty"`
|
||||
ContentEncoding string `json:"contentEncoding,omitempty"`
|
||||
Ref string `json:"$ref,omitempty"`
|
||||
}
|
||||
|
||||
// HasValidation returns true if at least one validator is set on the schema.
|
||||
// This excludes the schema's type but includes most other fields and can be
|
||||
// used to trigger additional slow validation steps when needed.
|
||||
func (s *Schema) HasValidation() bool {
|
||||
if s.Items != nil || len(s.Properties) > 0 || s.AdditionalProperties != nil || len(s.PatternProperties) > 0 || len(s.Required) > 0 || len(s.Enum) > 0 || s.Minimum != nil || s.ExclusiveMinimum != nil || s.Maximum != nil || s.ExclusiveMaximum != nil || s.MultipleOf != 0 || s.MinLength != nil || s.MaxLength != nil || s.Pattern != "" || s.MinItems != nil || s.MaxItems != nil || s.UniqueItems || s.MinProperties != nil || s.MaxProperties != nil || len(s.AllOf) > 0 || len(s.AnyOf) > 0 || len(s.OneOf) > 0 || s.Not != nil {
|
||||
if s.Items != nil || len(s.Properties) > 0 || s.AdditionalProperties != nil || len(s.PatternProperties) > 0 || len(s.Required) > 0 || len(s.Enum) > 0 || s.Minimum != nil || s.ExclusiveMinimum != nil || s.Maximum != nil || s.ExclusiveMaximum != nil || s.MultipleOf != 0 || s.MinLength != nil || s.MaxLength != nil || s.Pattern != "" || s.MinItems != nil || s.MaxItems != nil || s.UniqueItems || s.MinProperties != nil || s.MaxProperties != nil || len(s.AllOf) > 0 || len(s.AnyOf) > 0 || len(s.OneOf) > 0 || s.Not != nil || s.Ref != "" {
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue