Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,17 @@ func (pd ParameterDefinition) GoName() string {
return SchemaNameToTypeName(goName)
}

// Deprecated: Use HasOptionalPointer, as it is clearer what the intent is.
func (pd ParameterDefinition) IndirectOptional() bool {
return !pd.Required && !pd.Schema.SkipOptionalPointer
}

// HasOptionalPointer indicates whether the generated property has an optional pointer associated with it.
// This takes into account the `x-go-type-skip-optional-pointer` extension, allowing a parameter definition to control whether the pointer should be skipped.
func (pd ParameterDefinition) HasOptionalPointer() bool {
return pd.Required == false && pd.Schema.SkipOptionalPointer == false //nolint:staticcheck
}

type ParameterDefinitions []ParameterDefinition

func (p ParameterDefinitions) FindByName(name string) *ParameterDefinition {
Expand Down
6 changes: 6 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ func (p Property) GoTypeDef() string {
return typeDef
}

// HasOptionalPointer indicates whether the generated property has an optional pointer associated with it.
// This takes into account the `x-go-type-skip-optional-pointer` extension, allowing a parameter definition to control whether the pointer should be skipped.
func (p Property) HasOptionalPointer() bool {
return p.Required == false && p.Schema.SkipOptionalPointer == false //nolint:staticcheck
}

// EnumDefinition holds type information for enum
type EnumDefinition struct {
// Schema is the scheme of a type which has a list of enum values, eg, the
Expand Down
6 changes: 3 additions & 3 deletions pkg/codegen/templates/additional-properties.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func (a {{.TypeName}}) MarshalJSON() ([]byte, error) {
var err error
object := make(map[string]json.RawMessage)
{{range .Schema.Properties}}
{{if not .Required}}if a.{{.GoFieldName}} != nil { {{end}}
{{if .HasOptionalPointer}}if a.{{.GoFieldName}} != nil { {{end}}
object["{{.JsonFieldName}}"], err = json.Marshal(a.{{.GoFieldName}})
if err != nil {
return nil, fmt.Errorf("error marshaling '{{.JsonFieldName}}': %w", err)
}
{{if not .Required}} }{{end}}
{{if .HasOptionalPointer}} }{{end}}
{{end}}
for fieldName, field := range a.AdditionalProperties {
object[fieldName], err = json.Marshal(field)
Expand All @@ -69,4 +69,4 @@ func (a {{.TypeName}}) MarshalJSON() ([]byte, error) {
return json.Marshal(object)
}
{{end}}
{{end}}
{{end}}
14 changes: 7 additions & 7 deletions pkg/codegen/templates/chi/chi-middleware.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
if paramValue := r.URL.Query().Get("{{.ParamName}}"); paramValue != "" {

{{if .IsPassThrough}}
params.{{.GoName}} = {{if not .Required}}&{{end}}paramValue
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}paramValue
{{end}}

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

params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}
}{{if .Required}} else {
siw.ErrorHandlerFunc(w, r, &RequiredParamError{ParamName: "{{.ParamName}}"})
Expand Down Expand Up @@ -98,7 +98,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(w http.ResponseWriter, r *http.Requ
}

{{if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}valueList[0]
params.{{.GoName}} = {{if .HasOptionalPointer }}&{{end}}valueList[0]
{{end}}

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

params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}{{.GoName}}
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}{{.GoName}}

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

{{- if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}cookie.Value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}cookie.Value
{{end}}

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

params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}

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

}
Expand Down
30 changes: 15 additions & 15 deletions pkg/codegen/templates/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,20 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
if params != nil {
queryValues := queryURL.Query()
{{range $paramIdx, $param := .QueryParams}}
{{if not .Required}} if params.{{.GoName}} != nil { {{end}}
{{if .HasOptionalPointer}} if params.{{.GoName}} != nil { {{end}}
{{if .IsPassThrough}}
queryValues.Add("{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}})
queryValues.Add("{{.ParamName}}", {{if .HasOptionalPointer}}*{{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 .HasOptionalPointer}}*{{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 .HasOptionalPointer}}*{{end}}params.{{.GoName}}); err != nil {
return nil, err
} else if parsed, err := url.ParseQuery(queryFrag); err != nil {
return nil, err
Expand All @@ -222,7 +222,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
}
}
{{end}}
{{if not .Required}}}{{end}}
{{if .HasOptionalPointer}}}{{end}}
{{end}}
queryURL.RawQuery = queryValues.Encode()
}
Expand All @@ -236,49 +236,49 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
{{ if .HeaderParams }}
if params != nil {
{{range $paramIdx, $param := .HeaderParams}}
{{if not .Required}} if params.{{.GoName}} != nil { {{end}}
{{if .HasOptionalPointer}} if params.{{.GoName}} != nil { {{end}}
var headerParam{{$paramIdx}} string
{{if .IsPassThrough}}
headerParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
headerParam{{$paramIdx}} = {{if .HasOptionalPointer}}*{{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 .HasOptionalPointer}}*{{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 .HasOptionalPointer}}*{{end}}params.{{.GoName}})
if err != nil {
return nil, err
}
{{end}}
req.Header.Set("{{.ParamName}}", headerParam{{$paramIdx}})
{{if not .Required}}}{{end}}
{{if .HasOptionalPointer}}}{{end}}
{{end}}
}
{{- end }}{{/* if .HeaderParams */}}

{{ if .CookieParams }}
if params != nil {
{{range $paramIdx, $param := .CookieParams}}
{{if not .Required}} if params.{{.GoName}} != nil { {{end}}
{{if .HasOptionalPointer}} if params.{{.GoName}} != nil { {{end}}
var cookieParam{{$paramIdx}} string
{{if .IsPassThrough}}
cookieParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
cookieParam{{$paramIdx}} = {{if .HasOptionalPointer}}*{{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 .HasOptionalPointer}}*{{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 .HasOptionalPointer}}*{{end}}params.{{.GoName}})
if err != nil {
return nil, err
}
Expand All @@ -288,7 +288,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
Value:cookieParam{{$paramIdx}},
}
req.AddCookie(cookie{{$paramIdx}})
{{if not .Required}}}{{end}}
{{if .HasOptionalPointer}}}{{end}}
{{ end -}}
}
{{- end }}{{/* if .CookieParams */}}
Expand Down
14 changes: 7 additions & 7 deletions pkg/codegen/templates/echo/echo-wrappers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx echo.Context) error {
{{else}}
if paramValue := ctx.QueryParam("{{.ParamName}}"); paramValue != "" {
{{if .IsPassThrough}}
params.{{.GoName}} = {{if not .Required}}&{{end}}paramValue
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}paramValue
{{end}}
{{if .IsJson}}
var value {{.TypeDef}}
err = json.Unmarshal([]byte(paramValue), &value)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Error unmarshaling parameter '{{.ParamName}}' as JSON")
}
params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}
}{{if .Required}} else {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query argument {{.ParamName}} is required, but not found"))
Expand All @@ -70,7 +70,7 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Expected one value for {{.ParamName}}, got %d", n))
}
{{if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}valueList[0]
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}valueList[0]
{{end}}
{{if .IsJson}}
err = json.Unmarshal([]byte(valueList[0]), &{{.GoName}})
Expand All @@ -84,7 +84,7 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err))
}
{{end}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}{{.GoName}}
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}{{.GoName}}
} {{if .Required}}else {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Header parameter {{.ParamName}} is required, but not found"))
}{{end}}
Expand All @@ -94,7 +94,7 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx echo.Context) error {
{{range .CookieParams}}
if cookie, err := ctx.Cookie("{{.ParamName}}"); err == nil {
{{if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}cookie.Value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}cookie.Value
{{end}}
{{if .IsJson}}
var value {{.TypeDef}}
Expand All @@ -107,15 +107,15 @@ func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Error unmarshaling parameter '{{.ParamName}}' as JSON")
}
params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}
{{if .IsStyled}}
var value {{.TypeDef}}
err = runtime.BindStyledParameterWithOptions("simple", "{{.ParamName}}", cookie.Value, &value, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationCookie, Explode: {{.Explode}}, Required: {{.Required}}})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err))
}
params.{{.GoName}} = {{if not .Required}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}
}{{if .Required}} else {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query argument {{.ParamName}} is required, but not found"))
Expand Down
14 changes: 7 additions & 7 deletions pkg/codegen/templates/fiber/fiber-middleware.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
if paramValue := c.Query("{{.ParamName}}"); paramValue != "" {

{{if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}paramValue
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}paramValue
{{end}}

{{if .IsJson}}
Expand All @@ -69,7 +69,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Errorf("Error unmarshaling parameter '{{.ParamName}}' as JSON: %w", err).Error())
}

params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}
}{{if .Required}} else {
err = fmt.Errorf("Query argument {{.ParamName}} is required, but not found")
Expand All @@ -93,7 +93,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
var {{.GoName}} {{.TypeDef}}

{{if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}

{{if .IsJson}}
Expand All @@ -110,7 +110,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
}
{{end}}

params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}{{.GoName}}
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}{{.GoName}}

} {{if .Required}}else {
err = fmt.Errorf("Header parameter {{.ParamName}} is required, but not found: %w", err)
Expand All @@ -126,7 +126,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
if cookie = c.Cookies("{{.ParamName}}"); cookie == "" {

{{- if .IsPassThrough}}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}}&{{end}}cookie
params.{{.GoName}} = {{if .HasOptionalPointer}}}&{{end}}cookie
{{end}}

{{- if .IsJson}}
Expand All @@ -142,7 +142,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Errorf("Error unmarshaling parameter '{{.ParamName}}' as JSON: %w", err).Error())
}

params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}

{{- if .IsStyled}}
Expand All @@ -151,7 +151,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *fiber.Ctx) error {
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Errorf("Invalid format for parameter {{.ParamName}}: %w", err).Error())
}
params.{{.GoName}} = {{if and (not .Required) (not .Schema.SkipOptionalPointer)}}&{{end}}value
params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value
{{end}}

}
Expand Down
Loading