diff --git a/.versions b/.versions index 1218535..8655a83 100644 --- a/.versions +++ b/.versions @@ -1,3 +1,3 @@ -APP_VERSION: 1.2.4 +APP_VERSION: 0.0.6 OPENAPI_GENERATOR_CLI: 7.6.0 MAVEN: 3.8.8 \ No newline at end of file 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/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 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 4412a6c..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 @@ -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/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..8d67a9b 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,26 @@ 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) { + runRouteTestCase(t, "should process the request", setupRouter, func() testCase { + return testCase{ + method: http.MethodGet, + path: "/behavior/no-params-no-response", + expect: func(t *testing.T, testActions *behaviorControllerTestActions, recorder *httptest.ResponseRecorder) { + if !assert.Equal(t, 202, recorder.Code, "Unexpected response: %v", recorder.Body) { + return + } + + assert.Len(t, testActions.noParamsNoResponse.calls, 1) + }, + } + }) + }) } 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 36ef30e..821161c 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 makeVoidParamsParser(_ *HTTPApp) paramsParser[voidValue] { + return voidParamsParser{} +} + type handlerFactoryParams[TReqParams any, TResData any] struct { defaultStatus int voidResult bool