Skip to content

Commit 248e33d

Browse files
committed
[go-server] Set default values in object properties
When a default value is set for an object property, ensure it is set into the struct before decoding the JSON body. Fix #4579
1 parent c1d0e7c commit 248e33d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+339
-58
lines changed

modules/openapi-generator/src/main/resources/go-server/controller-api.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,12 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
591591
{{paramName}}Param := r.Header.Get("{{baseName}}")
592592
{{/isHeaderParam}}
593593
{{#isBodyParam}}
594+
{{#isArray}}
594595
{{paramName}}Param := {{dataType}}{}
596+
{{/isArray}}
597+
{{^isArray}}
598+
{{paramName}}Param := New{{dataType}}WithDefaults()
599+
{{/isArray}}
595600
d := json.NewDecoder(r.Body)
596601
{{^isAdditionalPropertiesTrue}}
597602
d.DisallowUnknownFields()

modules/openapi-generator/src/main/resources/go-server/model.mustache

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,34 @@ type {{classname}} struct {
7474
{{/deprecated}}
7575
{{name}} {{#isNullable}}*{{/isNullable}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
7676
{{/vars}}
77-
}{{/isEnum}}
77+
}
78+
79+
{{^isArray}}
80+
// New{{classname}}WithDefaults instantiates a new {{classname}} object
81+
// This constructor will only assign default values to properties that have it defined,
82+
// but it doesn't guarantee that properties required by API are set
83+
func New{{classname}}WithDefaults() {{classname}} {
84+
this := {{classname}}{}
85+
{{#parent}}
86+
{{^isMap}}
87+
{{^isArray}}
88+
this.{{{parent}}} = New{{{parent}}}WithDefaults()
89+
{{/isArray}}
90+
{{/isMap}}
91+
{{/parent}}
92+
{{#vars}}
93+
{{#defaultValue}}
94+
{{^isArray}}
95+
this.{{name}} = {{#isBoolean}}{{{.}}}{{/isBoolean}}{{#isNumeric}}{{{.}}}{{/isNumeric}}{{^isBoolean}}{{^isNumeric}}"{{{.}}}"{{/isNumeric}}{{/isBoolean}}
96+
{{/isArray}}
97+
{{/defaultValue}}
98+
{{#isModel}}
99+
{{#isNullable}}*{{/isNullable}}this.{{name}} = New{{dataType}}WithDefaults()
100+
{{/isModel}}
101+
{{/vars}}
102+
return this
103+
}{{/isArray}}
104+
{{/isEnum}}
78105

79106
// Assert{{classname}}Required checks if the required fields are not zero-ed
80107
func Assert{{classname}}Required(obj {{classname}}) error {

modules/openapi-generator/src/test/resources/3_0/go-server/petstore_with_test_endpoint.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ components:
651651
quantity:
652652
type: integer
653653
format: int32
654+
default: 1
654655
shipDate:
655656
type: string
656657
format: date-time
@@ -669,6 +670,7 @@ components:
669670
type: string
670671
type:
671672
type: string
673+
default: "dog"
672674
required:
673675
- requireTest
674676
xml:
@@ -687,9 +689,6 @@ components:
687689
petId:
688690
type: integer
689691
format: int64
690-
quantity:
691-
type: integer
692-
format: int32
693692
shipDate:
694693
type: string
695694
format: date-time
@@ -700,6 +699,7 @@ components:
700699
- placed
701700
- approved
702701
- delivered
702+
default: "placed"
703703
complete:
704704
type: boolean
705705
default: false

samples/openapi3/server/petstore/go/api_store_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"strings"
99
"testing"
1010

11+
petstoreserver "go-petstore/go"
12+
1113
"github.com/stretchr/testify/assert"
1214
"github.com/stretchr/testify/require"
13-
petstoreserver "go-petstore/go"
1415
)
1516

1617
type StoreAPITestService struct {
@@ -61,9 +62,7 @@ func TestPlaceOrderOK(t *testing.T) {
6162
}
6263

6364
// Create the controller with the service
64-
router := petstoreserver.NewStoreAPIController(service)
65-
controller, ok := router.(*petstoreserver.StoreAPIController)
66-
require.True(t, ok)
65+
controller := petstoreserver.NewStoreAPIController(service)
6766

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

109108
// Create the controller with the service
110-
router := petstoreserver.NewStoreAPIController(service)
111-
controller, ok := router.(*petstoreserver.StoreAPIController)
112-
require.True(t, ok)
109+
controller := petstoreserver.NewStoreAPIController(service)
113110

114111
// Call the method of controller we are testing
115112
controller.PlaceOrder(w, req)

samples/openapi3/server/petstore/go/api_user_test.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"strings"
99
"testing"
1010

11+
petstoreserver "go-petstore/go"
12+
1113
"github.com/stretchr/testify/assert"
1214
"github.com/stretchr/testify/require"
13-
petstoreserver "go-petstore/go"
1415
)
1516

1617
type UserAPITestService struct {
@@ -115,9 +116,7 @@ func TestCreateUserOK(t *testing.T) {
115116
}
116117

117118
// Create the controller with the service
118-
router := petstoreserver.NewUserAPIController(service)
119-
controller, ok := router.(*petstoreserver.UserAPIController)
120-
require.True(t, ok)
119+
controller := petstoreserver.NewUserAPIController(service)
121120

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

216215
// Create the controller with the service
217-
router := petstoreserver.NewUserAPIController(service)
218-
controller, ok := router.(*petstoreserver.UserAPIController)
219-
require.True(t, ok)
216+
controller := petstoreserver.NewUserAPIController(service)
220217

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

260257
// Create the controller with the service
261-
router := petstoreserver.NewUserAPIController(service)
262-
controller, ok := router.(*petstoreserver.UserAPIController)
263-
require.True(t, ok)
258+
controller := petstoreserver.NewUserAPIController(service)
264259

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

331-
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)
326+
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)
332327
w := httptest.NewRecorder()
333328

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

347342
// Create the controller with the service
348-
router := petstoreserver.NewUserAPIController(service)
349-
controller, ok := router.(*petstoreserver.UserAPIController)
350-
require.True(t, ok)
343+
controller := petstoreserver.NewUserAPIController(service)
351344

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

370-
req := httptest.NewRequest(http.MethodPost, "/user/login?username=test", nil)
363+
req := httptest.NewRequest(http.MethodPost, "/user/login?username=test&password=test", nil)
371364
w := httptest.NewRecorder()
372365

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

387380
// Create the controller with the service
388-
router := petstoreserver.NewUserAPIController(service)
389-
controller, ok := router.(*petstoreserver.UserAPIController)
390-
require.True(t, ok)
381+
controller := petstoreserver.NewUserAPIController(service)
391382

392383
// Call the method of controller we are testing
393384
controller.LoginUser(w, req)

samples/openapi3/server/petstore/go/go-petstore/api/openapi.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ components:
662662
format: int64
663663
type: integer
664664
quantity:
665+
default: 1
665666
format: int32
666667
type: integer
667668
shipDate:
@@ -681,6 +682,7 @@ components:
681682
requireTest:
682683
type: string
683684
type:
685+
default: dog
684686
type: string
685687
required:
686688
- requireTest
@@ -695,7 +697,6 @@ components:
695697
description: An order for a pets from the pet store
696698
example:
697699
petId: 6
698-
quantity: 1
699700
comment: comment
700701
id: 0
701702
shipDate: 2000-01-23T04:56:07.000+00:00
@@ -708,13 +709,11 @@ components:
708709
petId:
709710
format: int64
710711
type: integer
711-
quantity:
712-
format: int32
713-
type: integer
714712
shipDate:
715713
format: date-time
716714
type: string
717715
status:
716+
default: placed
718717
description: Order Status
719718
enum:
720719
- placed

samples/openapi3/server/petstore/go/go-petstore/go/api_pet.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/api_store.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/api_user.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/model_an_object.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/model_api_response.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/model_category.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/model_order.go

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/model_order_info.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)