Skip to content

Commit

Permalink
feat: Don't make options array types *[]Type (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhapz authored Nov 1, 2021
1 parent 605b94d commit b9ac6df
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion examples/petstore-expanded/api/petstore.gen.go

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

2 changes: 1 addition & 1 deletion examples/petstore-expanded/api/petstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *PetStore) FindPets(w http.ResponseWriter, r *http.Request, params FindP
for _, pet := range p.Pets {
if params.Tags != nil {
// If we have tags, filter pets by tag
for _, t := range *params.Tags {
for _, t := range params.Tags {
if pet.Tag != nil && (*pet.Tag == t) {
result = append(result, pet)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/petstore-expanded/client/petstore-client.gen.go

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

32 changes: 16 additions & 16 deletions internal/test/parameters/parameters.gen.go

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

24 changes: 12 additions & 12 deletions internal/test/parameters/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ func (t *testServer) GetSimplePrimitive(w http.ResponseWriter, r *http.Request,
func (t *testServer) GetQueryForm(w http.ResponseWriter, r *http.Request, params GetQueryFormParams) {
t.queryParams = &params
if params.Ea != nil {
t.array = *params.Ea
t.array = params.Ea
}
if params.A != nil {
t.array = *params.A
t.array = params.A
}
if params.Eo != nil {
t.object = params.Eo
Expand Down Expand Up @@ -166,10 +166,10 @@ func (t *testServer) GetHeader(w http.ResponseWriter, r *http.Request, params Ge
t.primitive = params.XPrimitiveExploded
}
if params.XArray != nil {
t.array = *params.XArray
t.array = params.XArray
}
if params.XArrayExploded != nil {
t.array = *params.XArrayExploded
t.array = params.XArrayExploded
}
if params.XObject != nil {
t.object = params.XObject
Expand All @@ -189,10 +189,10 @@ func (t *testServer) GetHeader(w http.ResponseWriter, r *http.Request, params Ge
func (t *testServer) GetCookie(w http.ResponseWriter, r *http.Request, params GetCookieParams) {
t.cookieParams = &params
if params.Ea != nil {
t.array = *params.Ea
t.array = params.Ea
}
if params.A != nil {
t.array = *params.A
t.array = params.A
}
if params.Eo != nil {
t.object = params.Eo
Expand Down Expand Up @@ -649,8 +649,8 @@ func TestClientQueryParams(t *testing.T) {

// Check query params
qParams := GetQueryFormParams{
Ea: &expectedArray1,
A: &expectedArray2,
Ea: expectedArray1,
A: expectedArray2,
Eo: &expectedObject1,
O: &expectedObject2,
Ep: &expectedPrimitive1,
Expand All @@ -669,8 +669,8 @@ func TestClientQueryParams(t *testing.T) {

// Check cookie params
cParams := GetCookieParams{
Ea: &expectedArray1,
A: &expectedArray2,
Ea: expectedArray1,
A: expectedArray2,
Eo: &expectedObject1,
O: &expectedObject2,
Ep: &expectedPrimitive1,
Expand All @@ -687,8 +687,8 @@ func TestClientQueryParams(t *testing.T) {

// Check Header parameters
hParams := GetHeaderParams{
XArrayExploded: &expectedArray1,
XArray: &expectedArray2,
XArrayExploded: expectedArray1,
XArray: expectedArray2,
XObjectExploded: &expectedObject1,
XObject: &expectedObject2,
XPrimitiveExploded: &expectedPrimitive1,
Expand Down
6 changes: 3 additions & 3 deletions internal/test/server/server.gen.go

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

2 changes: 2 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func resolveType(schema *openapi3.Schema, path []string, outSchema *Schema) erro
outSchema.GoType = "[]" + arrayType.TypeDecl()
outSchema.AdditionalTypes = arrayType.AdditionalTypes
outSchema.Properties = arrayType.Properties
outSchema.SkipOptionalPointer = true
case "integer":
// We default to int if format doesn't ask for something else.
switch f {
Expand Down Expand Up @@ -400,6 +401,7 @@ func resolveType(schema *openapi3.Schema, path []string, outSchema *Schema) erro
switch f {
case "byte":
outSchema.GoType = "[]byte"
outSchema.SkipOptionalPointer = true
case "email":
outSchema.GoType = "openapi_types.Email"
case "date":
Expand Down
18 changes: 9 additions & 9 deletions pkg/codegen/templates/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,18 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
{{range $paramIdx, $param := .QueryParams}}
{{if not .Required}} if params.{{.GoName}} != nil { {{end}}
{{if .IsPassThrough}}
queryValues.Add("{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}})
queryValues.Add("{{.ParamName}}", {{if .IndirectOptional}}*{{end}}params.{{.GoName}})
{{end}}
{{if .IsJSON}}
if queryParamBuf, err := json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {
if queryParamBuf, err := json.Marshal({{if .IndirectOptional}}*{{end}}params.{{.GoName}}); err != nil {
return nil, err
} else {
queryValues.Add("{{.ParamName}}", string(queryParamBuf))
}

{{end}}
{{if .IsStyled}}
if queryFrag, err := runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationQuery, {{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {
if queryFrag, err := runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationQuery, {{if .IndirectOptional}}*{{end}}params.{{.GoName}}); err != nil {
return nil, err
} else if parsed, err := url.ParseQuery(queryFrag); err != nil {
return nil, err
Expand All @@ -214,18 +214,18 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
{{if not .Required}} if params.{{.GoName}} != nil { {{end}}
var headerParam{{$paramIdx}} string
{{if .IsPassThrough}}
headerParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
headerParam{{$paramIdx}} = {{if .IndirectOptional}}*{{end}}params.{{.GoName}}
{{end}}
{{if .IsJSON}}
var headerParamBuf{{$paramIdx}} []byte
headerParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}})
headerParamBuf{{$paramIdx}}, err = json.Marshal({{if .IndirectOptional}}*{{end}}params.{{.GoName}})
if err != nil {
return nil, err
}
headerParam{{$paramIdx}} = string(headerParamBuf{{$paramIdx}})
{{end}}
{{if .IsStyled}}
headerParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationHeader, {{if not .Required}}*{{end}}params.{{.GoName}})
headerParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("{{.Style}}", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationHeader, {{if .IndirectOptional}}*{{end}}params.{{.GoName}})
if err != nil {
return nil, err
}
Expand All @@ -238,18 +238,18 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
{{if not .Required}} if params.{{.GoName}} != nil { {{end}}
var cookieParam{{$paramIdx}} string
{{if .IsPassThrough}}
cookieParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
cookieParam{{$paramIdx}} = {{if .IndirectOptional}}*{{end}}params.{{.GoName}}
{{end}}
{{if .IsJSON}}
var cookieParamBuf{{$paramIdx}} []byte
cookieParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}})
cookieParamBuf{{$paramIdx}}, err = json.Marshal({{if .IndirectOptional}}*{{end}}params.{{.GoName}})
if err != nil {
return nil, err
}
cookieParam{{$paramIdx}} = url.QueryEscape(string(cookieParamBuf{{$paramIdx}}))
{{end}}
{{if .IsStyled}}
cookieParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("simple", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationCookie, {{if not .Required}}*{{end}}params.{{.GoName}})
cookieParam{{$paramIdx}}, err = runtime.StyleParamWithLocation("simple", {{.Explode}}, "{{.ParamName}}", runtime.ParamLocationCookie, {{if .IndirectOptional}}*{{end}}params.{{.GoName}})
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/codegen/templates/middleware.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
{{else}}
if paramValue := r.URL.Query().Get("{{.ParamName}}"); paramValue != "" {
{{if .IsPassThrough}}
params.{{.GoName}} = {{if not .Required}}&{{end}}paramValue
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}paramValue
{{end}}
{{if .IsJSON}}
var value {{.TypeDef}}
Expand All @@ -61,7 +61,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
siw.ErrorHandlerFunc(w, r, &UnmarshalingParamError{err})
return
}
params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}value
{{end}}
}{{if .Required}} else {
err := fmt.Errorf("query argument {{.ParamName}} is required, but not found")
Expand All @@ -85,7 +85,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
}

{{if .IsPassThrough}}
params.{{.GoName}} = {{if not .Required}}&{{end}}valueList[0]
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}valueList[0]
{{end}}

{{if .IsJSON}}
Expand All @@ -104,7 +104,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
}
{{end}}

params.{{.GoName}} = {{if not .Required}}&{{end}}{{.GoName}}
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}{{.GoName}}

} {{if .Required}}else {
err := fmt.Errorf("header parameter {{.ParamName}} is required, but not found")
Expand All @@ -119,7 +119,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
if cookie, err := r.Cookie("{{.ParamName}}"); err == nil {

{{- if .IsPassThrough}}
params.{{.GoName}} = {{if not .Required}}&{{end}}cookie.Value
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}cookie.Value
{{end}}

{{- if .IsJSON}}
Expand All @@ -139,7 +139,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
return
}

params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}value
{{end}}

{{- if .IsStyled}}
Expand All @@ -149,7 +149,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{err})
return
}
params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .IndirectOptional}}{{if not .Required}}&{{end}}{{end}}value
{{end}}

}
Expand Down
Loading

0 comments on commit b9ac6df

Please sign in to comment.