From 276d9791ddeaca955ba2600743066673e9e4e115 Mon Sep 17 00:00:00 2001 From: Eugene Myasyshchev Date: Sat, 7 Dec 2024 19:10:34 +0100 Subject: [PATCH 1/6] Simulating failure. --- tests/golang/controllers/behavior.go | 12 ++++++-- tests/golang/controllers/behavior_test.go | 36 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/tests/golang/controllers/behavior.go b/tests/golang/controllers/behavior.go index 5d313f1..59a0fd8 100644 --- a/tests/golang/controllers/behavior.go +++ b/tests/golang/controllers/behavior.go @@ -7,11 +7,17 @@ import ( "github.com/gemyago/apigen/tests/golang/routes/models" ) -func newBehaviorController() *handlers.BehaviorController { +type behaviorControllerTestActions struct { + noParamsNoResponse mockAction[struct{}] +} + +func newBehaviorController( + testActions *behaviorControllerTestActions, +) *handlers.BehaviorController { return handlers.BuildBehaviorController(). HandleBehaviorNoParamsNoResponse.With( - func(context.Context) error { - panic("not implemented") + func(ctx context.Context) error { + return testActions.noParamsNoResponse.action(ctx, struct{}{}) }). HandleBehaviorNoParamsWithResponse.With( func(context.Context) (*models.BehaviorNoParamsWithResponse202Response, error) { diff --git a/tests/golang/controllers/behavior_test.go b/tests/golang/controllers/behavior_test.go index 2a56de0..0ee1996 100644 --- a/tests/golang/controllers/behavior_test.go +++ b/tests/golang/controllers/behavior_test.go @@ -1,6 +1,8 @@ package controllers import ( + "net/http" + "net/http/httptest" "testing" "github.com/gemyago/apigen/tests/golang/routes/handlers" @@ -8,6 +10,16 @@ import ( ) func TestBehavior(t *testing.T) { + setupRouter := func() (*behaviorControllerTestActions, http.Handler) { + testActions := &behaviorControllerTestActions{} + controller := newBehaviorController(testActions) + router := &routerAdapter{ + mux: http.NewServeMux(), + } + handlers.RegisterBehaviorRoutes(controller, handlers.NewHTTPApp(router, handlers.WithLogger(newLogger()))) + return testActions, router.mux + } + t.Run("controller builder", func(t *testing.T) { t.Run("should panic if actions are not initialized", func(t *testing.T) { assert.PanicsWithError(t, "behaviorNoParamsNoResponse action has not been initialized", func() { @@ -17,8 +29,30 @@ func TestBehavior(t *testing.T) { t.Run("should build the controller if all actions are initialized", func(t *testing.T) { assert.NotPanics(t, func() { - newBehaviorController() + newBehaviorController(&behaviorControllerTestActions{}) }) }) }) + + type testCase = routeTestCase[*behaviorControllerTestActions] + + t.Run("noParamsNoResponse", func(t *testing.T) { + t.Run("should process the request", func(t *testing.T) { + + }) + + runRouteTestCase(t, "should process the request", setupRouter, func() testCase { + return testCase{ + method: http.MethodPost, + path: "/behavior/no-params-no-response", + expect: func(t *testing.T, testActions *behaviorControllerTestActions, recorder *httptest.ResponseRecorder) { + if !assert.Equal(t, 204, recorder.Code, "Unexpected response: %v", recorder.Body) { + return + } + + assert.Len(t, testActions.noParamsNoResponse.calls, 1) + }, + } + }) + }) } From 151ed3423a04b3f1f79f54cdc78c213acccf8724 Mon Sep 17 00:00:00 2001 From: Eugene Myasyshchev Date: Sat, 7 Dec 2024 19:38:51 +0100 Subject: [PATCH 2/6] Failing test. --- tests/golang/controllers/behavior_test.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/golang/controllers/behavior_test.go b/tests/golang/controllers/behavior_test.go index 0ee1996..8d67a9b 100644 --- a/tests/golang/controllers/behavior_test.go +++ b/tests/golang/controllers/behavior_test.go @@ -37,16 +37,12 @@ func TestBehavior(t *testing.T) { type testCase = routeTestCase[*behaviorControllerTestActions] t.Run("noParamsNoResponse", func(t *testing.T) { - t.Run("should process the request", func(t *testing.T) { - - }) - runRouteTestCase(t, "should process the request", setupRouter, func() testCase { return testCase{ - method: http.MethodPost, + method: http.MethodGet, path: "/behavior/no-params-no-response", expect: func(t *testing.T, testActions *behaviorControllerTestActions, recorder *httptest.ResponseRecorder) { - if !assert.Equal(t, 204, recorder.Code, "Unexpected response: %v", recorder.Body) { + if !assert.Equal(t, 202, recorder.Code, "Unexpected response: %v", recorder.Body) { return } From a638f6ca73600fc9da0945808397a5e1e89c4ff3 Mon Sep 17 00:00:00 2001 From: Eugene Myasyshchev Date: Sat, 7 Dec 2024 19:58:26 +0100 Subject: [PATCH 3/6] Revert version. --- .versions | 2 +- generators/go-apigen-server/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.versions b/.versions index 1218535..8038290 100644 --- a/.versions +++ b/.versions @@ -1,3 +1,3 @@ -APP_VERSION: 1.2.4 +APP_VERSION: 0.0.5 OPENAPI_GENERATOR_CLI: 7.6.0 MAVEN: 3.8.8 \ No newline at end of file diff --git a/generators/go-apigen-server/pom.xml b/generators/go-apigen-server/pom.xml index 5e4f623..86297ee 100644 --- a/generators/go-apigen-server/pom.xml +++ b/generators/go-apigen-server/pom.xml @@ -5,7 +5,7 @@ server jar Golang Server API Generator - 0.0.1 + 0.0.5 From 69187766394b5407d785a38fba26dc0fd1a1f8e6 Mon Sep 17 00:00:00 2001 From: Eugene Myasyshchev Date: Sat, 7 Dec 2024 19:58:45 +0100 Subject: [PATCH 4/6] Adding noop void params parser --- .../main/resources/go-apigen-server/handlers.mustache | 10 ++++++++++ tests/golang/routes/handlers/handlers.go | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache b/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache index 4412a6c..96627b6 100644 --- a/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache +++ b/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache @@ -115,6 +115,16 @@ type paramsParser[TReqParams any] interface { } type paramsParserFactory[TReqParams any] func(app *HTTPApp) paramsParser[TReqParams] +type voidParamsParser struct{} + +func (p voidParamsParser) parse(_ httpRouter, _ *http.Request) (voidValue, error) { + return voidValue(nil), nil +} + +func voidParamsParserFactory(_ *HTTPApp) paramsParser[voidValue] { + return voidParamsParser{} +} + type handlerFactoryParams[TReqParams any, TResData any] struct { defaultStatus int voidResult bool diff --git a/tests/golang/routes/handlers/handlers.go b/tests/golang/routes/handlers/handlers.go index 36ef30e..e44ed09 100644 --- a/tests/golang/routes/handlers/handlers.go +++ b/tests/golang/routes/handlers/handlers.go @@ -113,6 +113,16 @@ type paramsParser[TReqParams any] interface { } type paramsParserFactory[TReqParams any] func(app *HTTPApp) paramsParser[TReqParams] +type voidParamsParser struct{} + +func (p voidParamsParser) parse(_ httpRouter, _ *http.Request) (voidValue, error) { + return voidValue(nil), nil +} + +func voidParamsParserFactory(_ *HTTPApp) paramsParser[voidValue] { + return voidParamsParser{} +} + type handlerFactoryParams[TReqParams any, TResData any] struct { defaultStatus int voidResult bool From d3dc1611c4c9ae29167fb302dbd3d5db916af62f Mon Sep 17 00:00:00 2001 From: Eugene Myasyshchev Date: Sat, 7 Dec 2024 20:06:33 +0100 Subject: [PATCH 5/6] Proper generator for void params. --- .../pkg/api/http/v1routes/handlers/handlers.go | 10 ++++++++++ .../resources/go-apigen-server/controller.mustache | 4 ++-- .../main/resources/go-apigen-server/handlers.mustache | 2 +- tests/golang/routes/handlers/behavior_controller.go | 4 ++++ tests/golang/routes/handlers/handlers.go | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/go-apigen-server/pkg/api/http/v1routes/handlers/handlers.go b/examples/go-apigen-server/pkg/api/http/v1routes/handlers/handlers.go index 8c69a97..331955c 100644 --- a/examples/go-apigen-server/pkg/api/http/v1routes/handlers/handlers.go +++ b/examples/go-apigen-server/pkg/api/http/v1routes/handlers/handlers.go @@ -115,6 +115,16 @@ type paramsParser[TReqParams any] interface { } type paramsParserFactory[TReqParams any] func(app *HTTPApp) paramsParser[TReqParams] +type voidParamsParser struct{} + +func (p voidParamsParser) parse(_ httpRouter, _ *http.Request) (voidValue, error) { + return voidValue(nil), nil +} + +func makeVoidParamsParser(_ *HTTPApp) paramsParser[voidValue] { + return voidParamsParser{} +} + type handlerFactoryParams[TReqParams any, TResData any] struct { defaultStatus int voidResult bool diff --git a/generators/go-apigen-server/src/main/resources/go-apigen-server/controller.mustache b/generators/go-apigen-server/src/main/resources/go-apigen-server/controller.mustache index 6fc7a84..113772d 100644 --- a/generators/go-apigen-server/src/main/resources/go-apigen-server/controller.mustache +++ b/generators/go-apigen-server/src/main/resources/go-apigen-server/controller.mustache @@ -72,8 +72,8 @@ func Build{{classname}}Controller() *{{classname}}ControllerBuilder { // {{#lambda.uppercase}}{{httpMethod}}{{/lambda.uppercase}} {{{path}}} controllerBuilder.Handle{{operationId}}.controllerBuilder = controllerBuilder controllerBuilder.Handle{{operationId}}.defaultStatusCode = {{#responses.0}}{{ code }}{{/responses.0}}{{^responses.0}}200{{/responses.0}}{{^returnType}} - controllerBuilder.Handle{{operationId}}.voidResult = true{{/returnType}}{{#hasParams}} - controllerBuilder.Handle{{operationId}}.paramsParserFactory = newParamsParser{{baseName}}{{operationId}}{{/hasParams}} + controllerBuilder.Handle{{operationId}}.voidResult = true{{/returnType}} + controllerBuilder.Handle{{operationId}}.paramsParserFactory = {{#hasParams}}newParamsParser{{baseName}}{{operationId}}{{/hasParams}}{{^hasParams}}makeVoidParamsParser{{/hasParams}} {{/operation}} {{/operations}} diff --git a/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache b/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache index 96627b6..dcc4211 100644 --- a/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache +++ b/generators/go-apigen-server/src/main/resources/go-apigen-server/handlers.mustache @@ -121,7 +121,7 @@ func (p voidParamsParser) parse(_ httpRouter, _ *http.Request) (voidValue, error return voidValue(nil), nil } -func voidParamsParserFactory(_ *HTTPApp) paramsParser[voidValue] { +func makeVoidParamsParser(_ *HTTPApp) paramsParser[voidValue] { return voidParamsParser{} } diff --git a/tests/golang/routes/handlers/behavior_controller.go b/tests/golang/routes/handlers/behavior_controller.go index 8864a19..b784580 100644 --- a/tests/golang/routes/handlers/behavior_controller.go +++ b/tests/golang/routes/handlers/behavior_controller.go @@ -120,15 +120,18 @@ func BuildBehaviorController() *BehaviorControllerBuilder { controllerBuilder.HandleBehaviorNoParamsNoResponse.controllerBuilder = controllerBuilder controllerBuilder.HandleBehaviorNoParamsNoResponse.defaultStatusCode = 202 controllerBuilder.HandleBehaviorNoParamsNoResponse.voidResult = true + controllerBuilder.HandleBehaviorNoParamsNoResponse.paramsParserFactory = makeVoidParamsParser // GET /behavior/no-params-with-response controllerBuilder.HandleBehaviorNoParamsWithResponse.controllerBuilder = controllerBuilder controllerBuilder.HandleBehaviorNoParamsWithResponse.defaultStatusCode = 202 + controllerBuilder.HandleBehaviorNoParamsWithResponse.paramsParserFactory = makeVoidParamsParser // GET /behavior/no-status-defined controllerBuilder.HandleBehaviorNoStatusDefined.controllerBuilder = controllerBuilder controllerBuilder.HandleBehaviorNoStatusDefined.defaultStatusCode = 200 controllerBuilder.HandleBehaviorNoStatusDefined.voidResult = true + controllerBuilder.HandleBehaviorNoStatusDefined.paramsParserFactory = makeVoidParamsParser // GET /behavior/with-params-and-response controllerBuilder.HandleBehaviorWithParamsAndResponse.controllerBuilder = controllerBuilder @@ -139,6 +142,7 @@ func BuildBehaviorController() *BehaviorControllerBuilder { controllerBuilder.HandleBehaviorWithStatusDefined.controllerBuilder = controllerBuilder controllerBuilder.HandleBehaviorWithStatusDefined.defaultStatusCode = 202 controllerBuilder.HandleBehaviorWithStatusDefined.voidResult = true + controllerBuilder.HandleBehaviorWithStatusDefined.paramsParserFactory = makeVoidParamsParser return controllerBuilder } diff --git a/tests/golang/routes/handlers/handlers.go b/tests/golang/routes/handlers/handlers.go index e44ed09..821161c 100644 --- a/tests/golang/routes/handlers/handlers.go +++ b/tests/golang/routes/handlers/handlers.go @@ -119,7 +119,7 @@ func (p voidParamsParser) parse(_ httpRouter, _ *http.Request) (voidValue, error return voidValue(nil), nil } -func voidParamsParserFactory(_ *HTTPApp) paramsParser[voidValue] { +func makeVoidParamsParser(_ *HTTPApp) paramsParser[voidValue] { return voidParamsParser{} } From ac51a15f51a591dd58753886f29d883e270a9582 Mon Sep 17 00:00:00 2001 From: Eugene Myasyshchev Date: Sat, 7 Dec 2024 20:08:00 +0100 Subject: [PATCH 6/6] Version bump --- .versions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.versions b/.versions index 8038290..8655a83 100644 --- a/.versions +++ b/.versions @@ -1,3 +1,3 @@ -APP_VERSION: 0.0.5 +APP_VERSION: 0.0.6 OPENAPI_GENERATOR_CLI: 7.6.0 MAVEN: 3.8.8 \ No newline at end of file