Skip to content

Commit

Permalink
test: Added test for the generic controller
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Jan 8, 2025
1 parent a023af8 commit fce8558
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 25 additions & 0 deletions examples/petstore/controllers/pets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ func TestGetAllPets(t *testing.T) {
})
}

func TestGenericController(t *testing.T) {
t.Run("generic controller - validation issue", func(t *testing.T) {
s := lib.NewPetStoreServer()

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/pets/generic-response", nil)

s.Mux.ServeHTTP(w, r)

require.Equal(t, http.StatusBadRequest, w.Code)
})

t.Run("generic controller - success", func(t *testing.T) {
s := lib.NewPetStoreServer()

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/pets/generic-response", strings.NewReader(`{"data": {"id": "1", "name": "Napoleon"}}`))

s.Mux.ServeHTTP(w, r)

t.Log(w.Body.String())
require.Equal(t, http.StatusOK, w.Code)
})
}

func TestGetAllPetsStd(t *testing.T) {
t.Run("can get all pets std", func(t *testing.T) {
s := lib.NewPetStoreServer()
Expand Down
3 changes: 1 addition & 2 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,10 @@ func dive(openapi *OpenAPI, t reflect.Type, tag SchemaTag, maxDepth int) SchemaT
return tag

default:
tag.Name = t.Name()
tag.Name = transformTypeName(t.Name())
if t.Kind() == reflect.Struct && strings.HasPrefix(tag.Name, "DataOrTemplate") {
return dive(openapi, t.Field(0).Type, tag, maxDepth-1)
}
tag.Name = transformTypeName(tag.Name)
tag.Ref = "#/components/schemas/" + tag.Name
tag.Value = openapi.getOrCreateSchema(tag.Name, reflect.New(t).Interface())

Expand Down

0 comments on commit fce8558

Please sign in to comment.