From 52734f3fca237dc33bb0db0c4445e7f6d58ed945 Mon Sep 17 00:00:00 2001 From: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com> Date: Thu, 2 May 2024 16:14:06 +1000 Subject: [PATCH] Satisfy error and Validator interfaces for oneOf return types (#434) --- codegen/arrai/auto/svc_types.arrai | 13 ++++++++++++ .../cmd/gateway/main_test.go | 21 +++++++++++++++++-- .../specs/oneOf_backend.yaml | 8 +++++++ .../specs/oneOf_gateway.yaml | 8 +++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/codegen/arrai/auto/svc_types.arrai b/codegen/arrai/auto/svc_types.arrai index 82eca7c6..18ea4e2d 100644 --- a/codegen/arrai/auto/svc_types.arrai +++ b/codegen/arrai/auto/svc_types.arrai @@ -244,6 +244,19 @@ let fieldJsonTag = \type \key `::\i} } + ${cond {typename <: go.errorReturnTypes(app): $` + // Error fulfills the error interface. + func (s ${typename}) Error() string { + type plain ${typename} + return fmt.Sprintf("%+v", plain(s)) + } + `}} + + // *${typename} validator + func (s *${typename}) Validate() error { + return validator.Validate(s) + } + // Marshal data from the first non-nil pointer in the struct to JSON func (dst ${typename}) MarshalJSON() ([]byte, error) { ${unionTypes >> $` diff --git a/codegen/arrai/auto/tests/rest_miscellaneous/cmd/gateway/main_test.go b/codegen/arrai/auto/tests/rest_miscellaneous/cmd/gateway/main_test.go index 8b735e6c..2c4d4260 100644 --- a/codegen/arrai/auto/tests/rest_miscellaneous/cmd/gateway/main_test.go +++ b/codegen/arrai/auto/tests/rest_miscellaneous/cmd/gateway/main_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/anz-bank/sysl-go/validator" "rest_miscellaneous/internal/gen/pkg/servers/gateway" "rest_miscellaneous/internal/gen/pkg/servers/gateway/array_response_backend" "rest_miscellaneous/internal/gen/pkg/servers/gateway/encoder_backend" @@ -18,10 +19,11 @@ import ( "rest_miscellaneous/internal/gen/pkg/servers/gateway/oneof_backend" "rest_miscellaneous/internal/gen/pkg/servers/gateway/types" - "github.com/anz-bank/sysl-go/common" - "github.com/anz-bank/sysl-go/core" "github.com/sethvargo/go-retry" "github.com/stretchr/testify/require" + + "github.com/anz-bank/sysl-go/common" + "github.com/anz-bank/sysl-go/core" ) const applicationConfig = `--- @@ -307,6 +309,21 @@ func TestMiscellaneous_OneOfRaw(t *testing.T) { Send() } +func TestMiscellaneous_OneOfErrorReturn(t *testing.T) { + t.Parallel() + + // Just want to confirm that it generates types that satisfy the 'error' and 'Validator' interfaces + var e error + e = gateway.Post_rotateOneOf_400_resp_type_body{} + e = oneof_backend.Post_rotateOneOf_400_resp_type_body{} + _ = e + + var v validator.Validator + v = &gateway.Post_rotateOneOf_400_resp_type_body{} + v = &oneof_backend.Post_rotateOneOf_400_resp_type_body{} + _ = v +} + func TestMiscellaneous_MultiCode(t *testing.T) { t.Parallel() gatewayTester := gateway.NewTestServer(t, context.Background(), createService, "") diff --git a/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_backend.yaml b/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_backend.yaml index f0678d64..4902adb9 100644 --- a/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_backend.yaml +++ b/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_backend.yaml @@ -21,6 +21,14 @@ paths: application/json; charset = utf-8: schema: $ref: "#/components/schemas/OneOfResponse" + '400': + description: error + content: + application/json: + schema: + oneOf: + - $ref: "#/components/schemas/One" + - $ref: "#/components/schemas/Two" components: schemas: diff --git a/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_gateway.yaml b/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_gateway.yaml index 05c30f90..34d94f5e 100644 --- a/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_gateway.yaml +++ b/codegen/arrai/auto/tests/rest_miscellaneous/specs/oneOf_gateway.yaml @@ -28,6 +28,14 @@ paths: example: values: - one: true + '400': + description: error + content: + application/json: + schema: + oneOf: + - $ref: "#/components/schemas/One" + - $ref: "#/components/schemas/Two" components: schemas: