[go-server] Fix: missing quotes for string default value (#18546)

* Update controller mustache

* Add tests to openapi doc

* Regen
This commit is contained in:
Ween Jiann 2024-05-06 14:17:44 +08:00 committed by GitHub
parent b1fac19a75
commit cefbf62060
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 188 additions and 43 deletions

View File

@ -520,7 +520,7 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
{{/required}} {{/required}}
{{^required}} {{^required}}
{{#defaultValue}} {{#defaultValue}}
param := {{^isString}}{{dataType}}({{/isString}}{{defaultValue}}{{^isString}}){{/isString}} param := {{^isString}}{{dataType}}({{/isString}}"{{defaultValue}}"{{^isString}}){{/isString}}
{{paramName}}Param = {{#isNullable}}&{{/isNullable}}param {{paramName}}Param = {{#isNullable}}&{{/isNullable}}param
{{/defaultValue}} {{/defaultValue}}
{{/required}} {{/required}}

View File

@ -100,6 +100,21 @@ paths:
- "OPTION_1" - "OPTION_1"
- "OPTION_2" - "OPTION_2"
- "OPTION_3" - "OPTION_3"
- name: defaultInt
in: query
schema:
default: 1
type: integer
- name: defaultNum
in: query
schema:
default: 1.5
type: number
- name: defaultStr
in: query
schema:
default: default
type: string
- name: status - name: status
in: query in: query
description: Status values that need to be considered for filter description: Status values that need to be considered for filter
@ -616,9 +631,9 @@ paths:
required: true required: true
schema: schema:
type: string type: string
- name: boolean_test - name: remember_me
in: query in: query
description: The password for login in clear text description: Remember Me
schema: schema:
type: boolean type: boolean
responses: responses:
@ -731,9 +746,9 @@ paths:
required: true required: true
schema: schema:
type: string type: string
- name: boolean_test # to ensure boolean query parameter won't cause compilation errors - name: confirmation # to ensure boolean query parameter won't cause compilation errors
in: query in: query
description: boolean query parameter description: Confirm the deletion
schema: schema:
type: boolean type: boolean
responses: responses:

View File

@ -105,6 +105,30 @@ paths:
- OPTION_3 - OPTION_3
type: string type: string
style: form style: form
- explode: true
in: query
name: defaultInt
required: false
schema:
default: 1
type: integer
style: form
- explode: true
in: query
name: defaultNum
required: false
schema:
default: 1.5
type: number
style: form
- explode: true
in: query
name: defaultStr
required: false
schema:
default: default
type: string
style: form
- deprecated: true - deprecated: true
description: Status values that need to be considered for filter description: Status values that need to be considered for filter
explode: false explode: false
@ -631,10 +655,10 @@ paths:
schema: schema:
type: string type: string
style: form style: form
- description: The password for login in clear text - description: Remember Me
explode: true explode: true
in: query in: query
name: boolean_test name: remember_me
required: false required: false
schema: schema:
type: boolean type: boolean
@ -702,10 +726,10 @@ paths:
schema: schema:
type: string type: string
style: simple style: simple
- description: boolean query parameter - description: Confirm the deletion
explode: true explode: true
in: query in: query
name: boolean_test name: confirmation
required: false required: false
schema: schema:
type: boolean type: boolean

View File

@ -71,7 +71,7 @@ type PetAPIServicer interface {
AddPet(context.Context, Pet) (ImplResponse, error) AddPet(context.Context, Pet) (ImplResponse, error)
DeletePet(context.Context, int64, string) (ImplResponse, error) DeletePet(context.Context, int64, string) (ImplResponse, error)
FilterPetsByCategory(context.Context, Gender, Species, []Species) (ImplResponse, error) FilterPetsByCategory(context.Context, Gender, Species, []Species) (ImplResponse, error)
FindPetsByStatus(context.Context, []string, string, string) (ImplResponse, error) FindPetsByStatus(context.Context, []string, string, string, int32, float32, string) (ImplResponse, error)
// Deprecated // Deprecated
FindPetsByTags(context.Context, []string, time.Time, time.Time, Colour) (ImplResponse, error) FindPetsByTags(context.Context, []string, time.Time, time.Time, Colour) (ImplResponse, error)
GetPetById(context.Context, int64) (ImplResponse, error) GetPetById(context.Context, int64) (ImplResponse, error)

View File

@ -244,7 +244,48 @@ func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
inlineEnumParam = param inlineEnumParam = param
} else { } else {
} }
result, err := c.service.FindPetsByStatus(r.Context(), statusParam, inlineEnumPathParam, inlineEnumParam) var defaultIntParam int32
if query.Has("defaultInt") {
param, err := parseNumericParameter[int32](
query.Get("defaultInt"),
WithParse[int32](parseInt32),
)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
defaultIntParam = param
} else {
var param int32 = 1
defaultIntParam = param
}
var defaultNumParam float32
if query.Has("defaultNum") {
param, err := parseNumericParameter[float32](
query.Get("defaultNum"),
WithParse[float32](parseFloat32),
)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
defaultNumParam = param
} else {
var param float32 = 1.5
defaultNumParam = param
}
var defaultStrParam string
if query.Has("defaultStr") {
param := query.Get("defaultStr")
defaultStrParam = param
} else {
param := "default"
defaultStrParam = param
}
result, err := c.service.FindPetsByStatus(r.Context(), statusParam, inlineEnumPathParam, inlineEnumParam, defaultIntParam, defaultNumParam, defaultStrParam)
// If an error occurred, encode the error with the status code // If an error occurred, encode the error with the status code
if err != nil { if err != nil {
c.errorHandler(w, r, err, &result) c.errorHandler(w, r, err, &result)

View File

@ -69,7 +69,7 @@ func (s *PetAPIService) FilterPetsByCategory(ctx context.Context, gender Gender,
} }
// FindPetsByStatus - Finds Pets by status // FindPetsByStatus - Finds Pets by status
func (s *PetAPIService) FindPetsByStatus(ctx context.Context, status []string, inlineEnumPath string, inlineEnum string) (ImplResponse, error) { func (s *PetAPIService) FindPetsByStatus(ctx context.Context, status []string, inlineEnumPath string, inlineEnum string, defaultInt int32, defaultNum float32, defaultStr string) (ImplResponse, error) {
// TODO - update FindPetsByStatus with the required logic for this service method. // TODO - update FindPetsByStatus with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. // Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

View File

@ -184,10 +184,10 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &RequiredError{"username"}, nil) c.errorHandler(w, r, &RequiredError{"username"}, nil)
return return
} }
var booleanTestParam bool var confirmationParam bool
if query.Has("boolean_test") { if query.Has("confirmation") {
param, err := parseBoolParameter( param, err := parseBoolParameter(
query.Get("boolean_test"), query.Get("confirmation"),
WithParse[bool](parseBool), WithParse[bool](parseBool),
) )
if err != nil { if err != nil {
@ -195,10 +195,10 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return return
} }
booleanTestParam = param confirmationParam = param
} else { } else {
} }
result, err := c.service.DeleteUser(r.Context(), usernameParam, booleanTestParam) result, err := c.service.DeleteUser(r.Context(), usernameParam, confirmationParam)
// If an error occurred, encode the error with the status code // If an error occurred, encode the error with the status code
if err != nil { if err != nil {
c.errorHandler(w, r, err, &result) c.errorHandler(w, r, err, &result)
@ -251,10 +251,10 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &RequiredError{Field: "password"}, nil) c.errorHandler(w, r, &RequiredError{Field: "password"}, nil)
return return
} }
var booleanTestParam bool var rememberMeParam bool
if query.Has("boolean_test") { if query.Has("remember_me") {
param, err := parseBoolParameter( param, err := parseBoolParameter(
query.Get("boolean_test"), query.Get("remember_me"),
WithParse[bool](parseBool), WithParse[bool](parseBool),
) )
if err != nil { if err != nil {
@ -262,10 +262,10 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
return return
} }
booleanTestParam = param rememberMeParam = param
} else { } else {
} }
result, err := c.service.LoginUser(r.Context(), usernameParam, passwordParam, booleanTestParam) result, err := c.service.LoginUser(r.Context(), usernameParam, passwordParam, rememberMeParam)
// If an error occurred, encode the error with the status code // If an error occurred, encode the error with the status code
if err != nil { if err != nil {
c.errorHandler(w, r, err, &result) c.errorHandler(w, r, err, &result)

View File

@ -61,7 +61,7 @@ func (s *UserAPIService) CreateUsersWithListInput(ctx context.Context, user []Us
} }
// DeleteUser - Delete user // DeleteUser - Delete user
func (s *UserAPIService) DeleteUser(ctx context.Context, username string, booleanTest bool) (ImplResponse, error) { func (s *UserAPIService) DeleteUser(ctx context.Context, username string, confirmation bool) (ImplResponse, error) {
// TODO - update DeleteUser with the required logic for this service method. // TODO - update DeleteUser with the required logic for this service method.
// Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
@ -92,7 +92,7 @@ func (s *UserAPIService) GetUserByName(ctx context.Context, username string) (Im
} }
// LoginUser - Logs user into the system // LoginUser - Logs user into the system
func (s *UserAPIService) LoginUser(ctx context.Context, username string, password string, booleanTest bool) (ImplResponse, error) { func (s *UserAPIService) LoginUser(ctx context.Context, username string, password string, rememberMe bool) (ImplResponse, error) {
// TODO - update LoginUser with the required logic for this service method. // TODO - update LoginUser with the required logic for this service method.
// Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

View File

@ -105,6 +105,30 @@ paths:
- OPTION_3 - OPTION_3
type: string type: string
style: form style: form
- explode: true
in: query
name: defaultInt
required: false
schema:
default: 1
type: integer
style: form
- explode: true
in: query
name: defaultNum
required: false
schema:
default: 1.5
type: number
style: form
- explode: true
in: query
name: defaultStr
required: false
schema:
default: default
type: string
style: form
- deprecated: true - deprecated: true
description: Status values that need to be considered for filter description: Status values that need to be considered for filter
explode: false explode: false
@ -631,10 +655,10 @@ paths:
schema: schema:
type: string type: string
style: form style: form
- description: The password for login in clear text - description: Remember Me
explode: true explode: true
in: query in: query
name: boolean_test name: remember_me
required: false required: false
schema: schema:
type: boolean type: boolean
@ -702,10 +726,10 @@ paths:
schema: schema:
type: string type: string
style: simple style: simple
- description: boolean query parameter - description: Confirm the deletion
explode: true explode: true
in: query in: query
name: boolean_test name: confirmation
required: false required: false
schema: schema:
type: boolean type: boolean

View File

@ -71,7 +71,7 @@ type PetAPIServicer interface {
AddPet(context.Context, Pet) (ImplResponse, error) AddPet(context.Context, Pet) (ImplResponse, error)
DeletePet(context.Context, int64, string) (ImplResponse, error) DeletePet(context.Context, int64, string) (ImplResponse, error)
FilterPetsByCategory(context.Context, Gender, Species, []Species) (ImplResponse, error) FilterPetsByCategory(context.Context, Gender, Species, []Species) (ImplResponse, error)
FindPetsByStatus(context.Context, []string, string, string) (ImplResponse, error) FindPetsByStatus(context.Context, []string, string, string, int32, float32, string) (ImplResponse, error)
// Deprecated // Deprecated
FindPetsByTags(context.Context, []string, time.Time, time.Time, Colour) (ImplResponse, error) FindPetsByTags(context.Context, []string, time.Time, time.Time, Colour) (ImplResponse, error)
GetPetById(context.Context, int64) (ImplResponse, error) GetPetById(context.Context, int64) (ImplResponse, error)

View File

@ -241,7 +241,48 @@ func (c *PetAPIController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
inlineEnumParam = param inlineEnumParam = param
} else { } else {
} }
result, err := c.service.FindPetsByStatus(r.Context(), statusParam, inlineEnumPathParam, inlineEnumParam) var defaultIntParam int32
if query.Has("defaultInt") {
param, err := parseNumericParameter[int32](
query.Get("defaultInt"),
WithParse[int32](parseInt32),
)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
defaultIntParam = param
} else {
var param int32 = 1
defaultIntParam = param
}
var defaultNumParam float32
if query.Has("defaultNum") {
param, err := parseNumericParameter[float32](
query.Get("defaultNum"),
WithParse[float32](parseFloat32),
)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
defaultNumParam = param
} else {
var param float32 = 1.5
defaultNumParam = param
}
var defaultStrParam string
if query.Has("defaultStr") {
param := query.Get("defaultStr")
defaultStrParam = param
} else {
param := "default"
defaultStrParam = param
}
result, err := c.service.FindPetsByStatus(r.Context(), statusParam, inlineEnumPathParam, inlineEnumParam, defaultIntParam, defaultNumParam, defaultStrParam)
// If an error occurred, encode the error with the status code // If an error occurred, encode the error with the status code
if err != nil { if err != nil {
c.errorHandler(w, r, err, &result) c.errorHandler(w, r, err, &result)

View File

@ -69,7 +69,7 @@ func (s *PetAPIService) FilterPetsByCategory(ctx context.Context, gender Gender,
} }
// FindPetsByStatus - Finds Pets by status // FindPetsByStatus - Finds Pets by status
func (s *PetAPIService) FindPetsByStatus(ctx context.Context, status []string, inlineEnumPath string, inlineEnum string) (ImplResponse, error) { func (s *PetAPIService) FindPetsByStatus(ctx context.Context, status []string, inlineEnumPath string, inlineEnum string, defaultInt int32, defaultNum float32, defaultStr string) (ImplResponse, error) {
// TODO - update FindPetsByStatus with the required logic for this service method. // TODO - update FindPetsByStatus with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. // Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

View File

@ -183,10 +183,10 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &RequiredError{"username"}, nil) c.errorHandler(w, r, &RequiredError{"username"}, nil)
return return
} }
var booleanTestParam bool var confirmationParam bool
if query.Has("boolean_test") { if query.Has("confirmation") {
param, err := parseBoolParameter( param, err := parseBoolParameter(
query.Get("boolean_test"), query.Get("confirmation"),
WithParse[bool](parseBool), WithParse[bool](parseBool),
) )
if err != nil { if err != nil {
@ -194,10 +194,10 @@ func (c *UserAPIController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return return
} }
booleanTestParam = param confirmationParam = param
} else { } else {
} }
result, err := c.service.DeleteUser(r.Context(), usernameParam, booleanTestParam) result, err := c.service.DeleteUser(r.Context(), usernameParam, confirmationParam)
// If an error occurred, encode the error with the status code // If an error occurred, encode the error with the status code
if err != nil { if err != nil {
c.errorHandler(w, r, err, &result) c.errorHandler(w, r, err, &result)
@ -249,10 +249,10 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
c.errorHandler(w, r, &RequiredError{Field: "password"}, nil) c.errorHandler(w, r, &RequiredError{Field: "password"}, nil)
return return
} }
var booleanTestParam bool var rememberMeParam bool
if query.Has("boolean_test") { if query.Has("remember_me") {
param, err := parseBoolParameter( param, err := parseBoolParameter(
query.Get("boolean_test"), query.Get("remember_me"),
WithParse[bool](parseBool), WithParse[bool](parseBool),
) )
if err != nil { if err != nil {
@ -260,10 +260,10 @@ func (c *UserAPIController) LoginUser(w http.ResponseWriter, r *http.Request) {
return return
} }
booleanTestParam = param rememberMeParam = param
} else { } else {
} }
result, err := c.service.LoginUser(r.Context(), usernameParam, passwordParam, booleanTestParam) result, err := c.service.LoginUser(r.Context(), usernameParam, passwordParam, rememberMeParam)
// If an error occurred, encode the error with the status code // If an error occurred, encode the error with the status code
if err != nil { if err != nil {
c.errorHandler(w, r, err, &result) c.errorHandler(w, r, err, &result)

View File

@ -61,7 +61,7 @@ func (s *UserAPIService) CreateUsersWithListInput(ctx context.Context, user []Us
} }
// DeleteUser - Delete user // DeleteUser - Delete user
func (s *UserAPIService) DeleteUser(ctx context.Context, username string, booleanTest bool) (ImplResponse, error) { func (s *UserAPIService) DeleteUser(ctx context.Context, username string, confirmation bool) (ImplResponse, error) {
// TODO - update DeleteUser with the required logic for this service method. // TODO - update DeleteUser with the required logic for this service method.
// Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
@ -92,7 +92,7 @@ func (s *UserAPIService) GetUserByName(ctx context.Context, username string) (Im
} }
// LoginUser - Logs user into the system // LoginUser - Logs user into the system
func (s *UserAPIService) LoginUser(ctx context.Context, username string, password string, booleanTest bool) (ImplResponse, error) { func (s *UserAPIService) LoginUser(ctx context.Context, username string, password string, rememberMe bool) (ImplResponse, error) {
// TODO - update LoginUser with the required logic for this service method. // TODO - update LoginUser with the required logic for this service method.
// Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. // Add api_user_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.