Skip to content

Commit

Permalink
[go-server] Set default values in object properties
Browse files Browse the repository at this point in the history
When a default value is set for an object property, ensure it is set
into the struct before decoding the JSON body.

Fix OpenAPITools#4579
  • Loading branch information
rledisez committed May 28, 2024
1 parent c1d0e7c commit 248e33d
Show file tree
Hide file tree
Showing 52 changed files with 339 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,12 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
{{paramName}}Param := r.Header.Get("{{baseName}}")
{{/isHeaderParam}}
{{#isBodyParam}}
{{#isArray}}
{{paramName}}Param := {{dataType}}{}
{{/isArray}}
{{^isArray}}
{{paramName}}Param := New{{dataType}}WithDefaults()
{{/isArray}}
d := json.NewDecoder(r.Body)
{{^isAdditionalPropertiesTrue}}
d.DisallowUnknownFields()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,34 @@ type {{classname}} struct {
{{/deprecated}}
{{name}} {{#isNullable}}*{{/isNullable}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{/vars}}
}{{/isEnum}}
}

{{^isArray}}
// New{{classname}}WithDefaults instantiates a new {{classname}} object
// This constructor will only assign default values to properties that have it defined,
// but it doesn't guarantee that properties required by API are set
func New{{classname}}WithDefaults() {{classname}} {
this := {{classname}}{}
{{#parent}}
{{^isMap}}
{{^isArray}}
this.{{{parent}}} = New{{{parent}}}WithDefaults()
{{/isArray}}
{{/isMap}}
{{/parent}}
{{#vars}}
{{#defaultValue}}
{{^isArray}}
this.{{name}} = {{#isBoolean}}{{{.}}}{{/isBoolean}}{{#isNumeric}}{{{.}}}{{/isNumeric}}{{^isBoolean}}{{^isNumeric}}"{{{.}}}"{{/isNumeric}}{{/isBoolean}}
{{/isArray}}
{{/defaultValue}}
{{#isModel}}
{{#isNullable}}*{{/isNullable}}this.{{name}} = New{{dataType}}WithDefaults()
{{/isModel}}
{{/vars}}
return this
}{{/isArray}}
{{/isEnum}}

// Assert{{classname}}Required checks if the required fields are not zero-ed
func Assert{{classname}}Required(obj {{classname}}) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ components:
quantity:
type: integer
format: int32
default: 1
shipDate:
type: string
format: date-time
Expand All @@ -669,6 +670,7 @@ components:
type: string
type:
type: string
default: "dog"
required:
- requireTest
xml:
Expand All @@ -687,9 +689,6 @@ components:
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
Expand All @@ -700,6 +699,7 @@ components:
- placed
- approved
- delivered
default: "placed"
complete:
type: boolean
default: false
Expand Down
11 changes: 4 additions & 7 deletions samples/openapi3/server/petstore/go/api_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"strings"
"testing"

petstoreserver "go-petstore/go"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
petstoreserver "go-petstore/go"
)

type StoreAPITestService struct {
Expand Down Expand Up @@ -61,9 +62,7 @@ func TestPlaceOrderOK(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewStoreAPIController(service)
controller, ok := router.(*petstoreserver.StoreAPIController)
require.True(t, ok)
controller := petstoreserver.NewStoreAPIController(service)

// Call the method of controller we are testing
controller.PlaceOrder(w, req)
Expand Down Expand Up @@ -107,9 +106,7 @@ func TestPlaceOrderFailEmbeddedRequired(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewStoreAPIController(service)
controller, ok := router.(*petstoreserver.StoreAPIController)
require.True(t, ok)
controller := petstoreserver.NewStoreAPIController(service)

// Call the method of controller we are testing
controller.PlaceOrder(w, req)
Expand Down
27 changes: 9 additions & 18 deletions samples/openapi3/server/petstore/go/api_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"strings"
"testing"

petstoreserver "go-petstore/go"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
petstoreserver "go-petstore/go"
)

type UserAPITestService struct {
Expand Down Expand Up @@ -115,9 +116,7 @@ func TestCreateUserOK(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewUserAPIController(service)
controller, ok := router.(*petstoreserver.UserAPIController)
require.True(t, ok)
controller := petstoreserver.NewUserAPIController(service)

// Call the method of controller we are testing
controller.CreateUser(w, req)
Expand Down Expand Up @@ -214,9 +213,7 @@ func TestCreateUserOKNullablePhone(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewUserAPIController(service)
controller, ok := router.(*petstoreserver.UserAPIController)
require.True(t, ok)
controller := petstoreserver.NewUserAPIController(service)

// Call the method of controller we are testing
controller.CreateUser(w, req)
Expand Down Expand Up @@ -258,9 +255,7 @@ func TestCreateUserFailDeepSliceModelRequired(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewUserAPIController(service)
controller, ok := router.(*petstoreserver.UserAPIController)
require.True(t, ok)
controller := petstoreserver.NewUserAPIController(service)

// Call the method of controller we are testing
controller.CreateUser(w, req)
Expand Down Expand Up @@ -328,7 +323,7 @@ func TestLoginUserOK(t *testing.T) {
status = http.StatusOK
)

req := httptest.NewRequest(http.MethodPost, "/user/login?username=test&int32_test=1&int64_test=2&float32_test=1.1&float64_test=1.2&boolean_test=true", nil)
req := httptest.NewRequest(http.MethodPost, "/user/login?username=test&password=test&int32_test=1&int64_test=2&float32_test=1.1&float64_test=1.2&boolean_test=true", nil)
w := httptest.NewRecorder()

// Create the service and inject the logic
Expand All @@ -345,9 +340,7 @@ func TestLoginUserOK(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewUserAPIController(service)
controller, ok := router.(*petstoreserver.UserAPIController)
require.True(t, ok)
controller := petstoreserver.NewUserAPIController(service)

// Call the method of controller we are testing
controller.LoginUser(w, req)
Expand All @@ -367,7 +360,7 @@ func TestLoginUserOKOptional(t *testing.T) {
status = http.StatusOK
)

req := httptest.NewRequest(http.MethodPost, "/user/login?username=test", nil)
req := httptest.NewRequest(http.MethodPost, "/user/login?username=test&password=test", nil)
w := httptest.NewRecorder()

// Create the service and inject the logic
Expand All @@ -385,9 +378,7 @@ func TestLoginUserOKOptional(t *testing.T) {
}

// Create the controller with the service
router := petstoreserver.NewUserAPIController(service)
controller, ok := router.(*petstoreserver.UserAPIController)
require.True(t, ok)
controller := petstoreserver.NewUserAPIController(service)

// Call the method of controller we are testing
controller.LoginUser(w, req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ components:
format: int64
type: integer
quantity:
default: 1
format: int32
type: integer
shipDate:
Expand All @@ -681,6 +682,7 @@ components:
requireTest:
type: string
type:
default: dog
type: string
required:
- requireTest
Expand All @@ -695,7 +697,6 @@ components:
description: An order for a pets from the pet store
example:
petId: 6
quantity: 1
comment: comment
id: 0
shipDate: 2000-01-23T04:56:07.000+00:00
Expand All @@ -708,13 +709,11 @@ components:
petId:
format: int64
type: integer
quantity:
format: int32
type: integer
shipDate:
format: date-time
type: string
status:
default: placed
description: Order Status
enum:
- placed
Expand Down
4 changes: 2 additions & 2 deletions samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go

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

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

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

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

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

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

16 changes: 14 additions & 2 deletions samples/openapi3/server/petstore/go/go-petstore/go/model_order.go

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

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

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

Loading

0 comments on commit 248e33d

Please sign in to comment.