From c099653a6d91a51191e64b4b7cf93c7403ee5c30 Mon Sep 17 00:00:00 2001 From: zc Date: Fri, 22 Dec 2023 16:46:13 +0800 Subject: [PATCH 1/2] add bodyR to simplify body usage --- endpoint/endpoint.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index 50c1b94..abb4a06 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -192,6 +192,12 @@ func FormData(name string, typ types.ParameterType, description string, required } } +// BodyR defines a body parameter for the swagger endpoint as would commonly be used for the POST, PUT, and PATCH methods +// prototype should be a struct or a pointer to struct that swag can use to reflect upon the return type +func BodyR(prototype interface{}) Option { + return bodyType(reflect.TypeOf(prototype), "", true) +} + // Body defines a body parameter for the swagger endpoint as would commonly be used for the POST, PUT, and PATCH methods // prototype should be a struct or a pointer to struct that swag can use to reflect upon the return type func Body(prototype interface{}, description string, required bool) Option { From 66839dbdc583166b66b3b9e64c83c47ca3b3f89c Mon Sep 17 00:00:00 2001 From: zc Date: Fri, 22 Dec 2023 16:52:03 +0800 Subject: [PATCH 2/2] add tests --- endpoint/endpoint_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go index 380b3c1..17d6416 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -206,6 +206,27 @@ type Model struct { String string `json:"s"` } +func TestBodyR(t *testing.T) { + expected := swag.Parameter{ + In: "body", + Name: "body", + Required: true, + Schema: &swag.Schema{ + Ref: "#/definitions/endpoint.Model", + Prototype: reflect.TypeOf(Model{}), + }, + } + + e := New( + "get", "/", + Summary("get thing"), + BodyR(Model{}), + ) + + assert.Equal(t, 1, len(e.Parameters)) + assert.Equal(t, expected, e.Parameters[0]) +} + func TestBody(t *testing.T) { expected := swag.Parameter{ In: "body",