-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for dynamic schema and improve docs (#170)
- Loading branch information
Showing
19 changed files
with
282 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/bool64/ctxd" | ||
"github.com/swaggest/jsonschema-go" | ||
"github.com/swaggest/rest/request" | ||
"github.com/swaggest/usecase" | ||
"github.com/swaggest/usecase/status" | ||
) | ||
|
||
type dynamicInput struct { | ||
jsonschema.Struct | ||
request.EmbeddedSetter | ||
|
||
// Type is a static field example. | ||
Type string `query:"type"` | ||
} | ||
|
||
type dynamicOutput struct { | ||
// Embedded jsonschema.Struct exposes dynamic fields for documentation. | ||
jsonschema.Struct | ||
|
||
jsonFields map[string]interface{} | ||
headerFields map[string]string | ||
|
||
// Status is a static field example. | ||
Status string `json:"status"` | ||
} | ||
|
||
func (o dynamicOutput) SetupResponseHeader(h http.Header) { | ||
for k, v := range o.headerFields { | ||
h.Set(k, v) | ||
} | ||
} | ||
|
||
func (o dynamicOutput) MarshalJSON() ([]byte, error) { | ||
if o.jsonFields == nil { | ||
o.jsonFields = map[string]interface{}{} | ||
} | ||
|
||
o.jsonFields["status"] = o.Status | ||
|
||
return json.Marshal(o.jsonFields) | ||
} | ||
|
||
func dynamicSchema() usecase.Interactor { | ||
dynIn := dynamicInput{} | ||
dynIn.Struct.Fields = []jsonschema.Field{ | ||
{Name: "Foo", Value: 123, Tag: `header:"foo" enum:"123,456,789"`}, | ||
{Name: "Bar", Value: "abc", Tag: `query:"bar"`}, | ||
} | ||
|
||
dynOut := dynamicOutput{} | ||
dynOut.Struct.Fields = []jsonschema.Field{ | ||
{Name: "Foo", Value: 123, Tag: `header:"foo" enum:"123,456,789"`}, | ||
{Name: "Bar", Value: "abc", Tag: `json:"bar"`}, | ||
} | ||
|
||
u := usecase.NewIOI(dynIn, dynOut, func(ctx context.Context, input, output interface{}) (err error) { | ||
var ( | ||
in = input.(dynamicInput) | ||
out = output.(*dynamicOutput) | ||
) | ||
|
||
switch in.Type { | ||
case "ok": | ||
out.Status = "ok" | ||
out.jsonFields = map[string]interface{}{ | ||
"bar": in.Request().URL.Query().Get("bar"), | ||
} | ||
out.headerFields = map[string]string{ | ||
"foo": in.Request().Header.Get("foo"), | ||
} | ||
case "invalid_argument": | ||
return status.Wrap(errors.New("bad value for foo"), status.InvalidArgument) | ||
case "conflict": | ||
return status.Wrap(ctxd.NewError(ctx, "conflict", "foo", "bar"), | ||
status.AlreadyExists) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
u.SetTitle("Dynamic Request Schema") | ||
u.SetDescription("This use case demonstrates documentation of types that are only known at runtime.") | ||
u.SetExpectedErrors(status.InvalidArgument, status.FailedPrecondition, status.AlreadyExists) | ||
|
||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/swaggest/assertjson" | ||
) | ||
|
||
func Test_dynamicOutput(t *testing.T) { | ||
r := NewRouter() | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/dynamic-schema?bar=ccc&type=ok", nil) | ||
req.Header.Set("foo", "456") | ||
|
||
rw := httptest.NewRecorder() | ||
r.ServeHTTP(rw, req) | ||
|
||
assertjson.Equal(t, []byte(`{"bar": "ccc","status": "ok"}`), rw.Body.Bytes()) | ||
assert.Equal(t, http.StatusOK, rw.Code) | ||
assert.Equal(t, "456", rw.Header().Get("foo")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.