Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golang: fix routes without params #29

Merged
merged 6 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
APP_VERSION: 1.2.4
APP_VERSION: 0.0.6
OPENAPI_GENERATOR_CLI: 7.6.0
MAVEN: 3.8.8

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion generators/go-apigen-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>server</artifactId>
<packaging>jar</packaging>
<name>Golang Server API Generator</name>
<version>0.0.1</version>
<version>0.0.5</version>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions tests/golang/controllers/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 31 additions & 1 deletion tests/golang/controllers/behavior_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package controllers

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gemyago/apigen/tests/golang/routes/handlers"
"github.com/stretchr/testify/assert"
)

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() {
Expand All @@ -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)
},
}
})
})
}
4 changes: 4 additions & 0 deletions tests/golang/routes/handlers/behavior_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions tests/golang/routes/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading