From 09ba8cfa37224328763584acde7172f9af731783 Mon Sep 17 00:00:00 2001 From: Steve Slotnick Date: Mon, 9 May 2022 12:17:02 -0700 Subject: [PATCH] feat: allow hidden/undocumented resources --- resource.go | 12 ++++++++++++ resource_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 resource_test.go diff --git a/resource.go b/resource.go index 31778d7a..03294a2a 100644 --- a/resource.go +++ b/resource.go @@ -20,9 +20,15 @@ type Resource struct { operations []*Operation tags []string + + hidden bool } func (r *Resource) toOpenAPI(components *oaComponents) *gabs.Container { + if r.hidden { + return nil + } + doc := gabs.New() for _, sub := range r.subResources { @@ -110,3 +116,9 @@ func (r *Resource) SubResource(path string) *Resource { func (r *Resource) Tags(names ...string) { r.tags = append(r.tags, names...) } + +// Hidden removes this resource from the OpenAPI spec and documentation. It +// is intended to be used for things like health check endpoints. +func (r *Resource) Hidden() { + r.hidden = true +} diff --git a/resource_test.go b/resource_test.go new file mode 100644 index 00000000..8c52d85f --- /dev/null +++ b/resource_test.go @@ -0,0 +1,24 @@ +package huma + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResource(t *testing.T) { + r := Resource{ + path: "/test", + } + + assert.NotNil(t, r.toOpenAPI(&oaComponents{})) +} + +func TestHiddenResource(t *testing.T) { + r := Resource{ + path: "/test", + } + r.Hidden() + + assert.Nil(t, r.toOpenAPI(&oaComponents{})) +}