diff --git a/operation.go b/operation.go index fc5091f4..0108a6f1 100644 --- a/operation.go +++ b/operation.go @@ -56,6 +56,7 @@ type Operation struct { responses []Response maxBodyBytes int64 bodyReadTimeout time.Duration + deprecated bool } func newOperation(resource *Resource, method, id, docs string, responses []Response) *Operation { @@ -72,6 +73,7 @@ func newOperation(resource *Resource, method, id, docs string, responses []Respo maxBodyBytes: 1024 * 1024, // 15 second timeout by default bodyReadTimeout: resource.router.defaultBodyReadTimeout, + deprecated: false, } } @@ -85,6 +87,9 @@ func (o *Operation) toOpenAPI(components *oaComponents) *gabs.Container { if o.description != "" { doc.Set(o.description, "description") } + if o.deprecated { + doc.Set(o.deprecated, "deprecated") + } // Request params for _, param := range o.params { @@ -188,6 +193,12 @@ func (o *Operation) RequestSchema(s *schema.Schema) { o.RequestSchemaForContentType("application/json", s) } +// Deprecated marks the operation is deprecated, warning consumers should +// refrain from using this. +func (o *Operation) Deprecated() { + o.deprecated = true +} + func (o *Operation) RequestSchemaForContentType(ct string, s *schema.Schema) { if o.requests[ct] == nil { o.requests[ct] = &request{} diff --git a/operation_test.go b/operation_test.go new file mode 100644 index 00000000..1cffa54f --- /dev/null +++ b/operation_test.go @@ -0,0 +1,22 @@ +package huma + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestOperation(t *testing.T) { + op := Operation{} + + assert.Equal(t, op.toOpenAPI(&oaComponents{}).Data(), map[string]interface{}{ + "operationId": "", + }) +} + +func TestDeprecatedOperation(t *testing.T) { + op := Operation{} + op.Deprecated() + + assert.Contains(t, op.toOpenAPI(&oaComponents{}).Data(), "deprecated") +}