api/templates: align with go-swagger v1.32.3

Align our custom templates with the one from upstream;
https://github.com/go-swagger/go-swagger/tree/v0.32.3/generator/templates

- Add "TODO" comments in places where we applied patches to make it
  easier to compare with upstream, and to see where differences are
  intentional.
- Update the server/operations.gotmpl template;
  - Add the "generated" header in the right place, so that it's
    recognized as generated file.
  - Add back the upstream template code; instead of removing the
    code, put a "if false" block around it; this allows comparing
    our forked templates with upstream when updating the version
    of go-swagger (to make sure we're not missing fixes).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-11 11:29:15 +02:00
parent f5fec92ea7
commit bbdd24d145
5 changed files with 146 additions and 17 deletions

View File

@@ -1,3 +1,10 @@
{{/*
Custom "schema" template for Moby; it disables generating marshaling
and validation code, and contains a fix for CamelCase (IPv6 <--> IPV6).
The orignal template can be found at:
https://github.com/go-swagger/go-swagger/blob/v0.32.3/generator/templates/schema.gotmpl
*/}}
{{- if and .IsBaseType .IsExported (not .IsSuperAlias) }}
{{- template "schemaPolymorphic" . }}
{{- else if .IsSuperAlias }}
@@ -74,7 +81,7 @@
{{- end }}
{{ template "mapOrSliceGetter" . }}
{{- end }}
{{/* {{ template "schemaSerializer" . }} */}}
{{/* {{ template "schemaSerializer" . }} */}}{{/* TODO(moby): Disabled for moby/api */}}
{{- end }}
{{- if and .IncludeValidator (not .IsSuperAlias) (not .IsEmbedded) }}{{/* aliased types type A = B do not redefine methods */}}
{{- if and (not (or .IsInterface .IsStream)) (or .Required .HasValidations .HasBaseType) }}
@@ -91,15 +98,15 @@ func ({{ .ReceiverName }} {{ .GoType }}) Pointer() *{{ .GoType }} {
}
{{- end }}
{{- end }}
{{ if false }}{{ template "schemavalidator" . }}{{ end }}
{{- else if and false not (or .IsInterface .IsStream) }}
{{/* {{ template "schemavalidator" . }} */}}{{/* TODO(moby): Disabled for moby/api */}}
{{- else if and false not (or .IsInterface .IsStream) }}{{/* TODO(moby): Disabled for moby/api */}}
// Validate validates this {{ humanize .Name }}{{/* this schema implements the runtime.Validatable interface but has no validations to check */}}
func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) Validate(formats strfmt.Registry) error {
return nil
}
{{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}}
{{- end }}
{{- if false }}
{{- if false }}{{/* TODO(moby): Disabled for moby/api */}}
{{- if and (not (or .IsInterface .IsStream)) (or .HasContextValidations) }}
{{ template "schemacontextvalidator" . }}
{{- else if not (or .IsInterface .IsStream) }}
@@ -111,8 +118,8 @@ func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperti
{{- end }}
{{- end }}
{{- if .WantsMarshalBinary }}
{{/* {{ template "marshalBinarySerializer" . }} */}}
{{- end }}
{{/* {{ template "marshalBinarySerializer" . }} */}}{{/* TODO(moby): Disabled for moby/api */}}
{{- end }}{{/* TODO(moby): Disabled for moby/api */}}
{{- end }}
{{- define "mapOrSliceGetter" }}{{/* signature for AdditionalProperties and AdditionalItems getter funcs */}}
{{- if not .IsBaseType }}

View File

@@ -1,23 +1,133 @@
{{/*
Custom "operation" template for Moby; it disables all code generation
and only produces the API structs.
The orignal template can be found at:
https://github.com/go-swagger/go-swagger/blob/v0.32.3/generator/templates/server/operation.gotmpl
*/}}
// Code generated by go-swagger; DO NOT EDIT.
{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
package {{ .Package }}
// ----------------------------------------------------------------------------
// Code generated by `swagger generate operation`. DO NOT EDIT.
// Code generated by `swagger generate operation`.
//
// See hack/generate-swagger-api.sh
// ----------------------------------------------------------------------------
{{- if false }}{{/* TODO(moby): Disabled for moby/api */}}
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the generate command
import (
"context"
"net/http"
{{ range .DefaultImports }}{{ printf "%q" . }}
{{ end }}
{{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }}
{{ end }}
"net/http"
{{- if .ExtraSchemas }}
stderrors "errors"
{{- end }}
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/runtime/security"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
{{ imports .DefaultImports }}
{{ imports .Imports }}
)
// {{ pascalize .Name }}HandlerFunc turns a function with the right signature into a {{ humanize .Name }} handler
type {{ pascalize .Name }}HandlerFunc func({{ pascalize .Name }}Params{{ if .Authorized }}, {{ if .PrincipalIsNullable }}*{{ end }}{{ .Principal }}{{ end }})
{{- if $.ReturnErrors }}
{{- if $.StrictResponders }} ({{ pascalize .Name }}Responder, error) {{else}} (middleware.Responder, error) {{end}}
{{ else }}
{{- if $.StrictResponders }} {{ pascalize .Name }}Responder {{else}} middleware.Responder {{end}}
{{ end }}
// Handle executing the request and returning a response
func (fn {{ pascalize .Name }}HandlerFunc) Handle(params {{ pascalize .Name }}Params{{ if .Authorized }}, principal {{ if .PrincipalIsNullable }}*{{ end }}{{ .Principal }}{{ end }})
{{- if $.ReturnErrors }}
{{- if $.StrictResponders }} ({{ pascalize .Name }}Responder, error) {{else}} (middleware.Responder, error) {{end}} {
{{ else }}
{{- if $.StrictResponders }} {{ pascalize .Name }}Responder {{else}} middleware.Responder {{end}} {
{{ end -}}
return fn(params{{ if .Authorized }}, principal{{ end }})
}
// {{ pascalize .Name }}Handler interface for that can handle valid {{ humanize .Name }} params
type {{ pascalize .Name }}Handler interface {
Handle({{ pascalize .Name }}Params{{ if .Authorized }}, {{ if .PrincipalIsNullable }}*{{ end }}{{ .Principal }}{{ end }})
{{- if $.ReturnErrors }}
{{- if $.StrictResponders }} ({{ pascalize .Name }}Responder, error) {{else}} (middleware.Responder, error) {{end}}
{{ else }}
{{- if $.StrictResponders }} {{ pascalize .Name }}Responder {{else}} middleware.Responder {{end}}
{{ end }}
}
// New{{ pascalize .Name }} creates a new http.Handler for the {{ humanize .Name }} operation
func New{{ pascalize .Name }}(ctx *middleware.Context, handler {{ pascalize .Name }}Handler) *{{ pascalize .Name }} {
return &{{ pascalize .Name }}{Context: ctx, Handler: handler}
}
/* {{ pascalize .Name }} swagger:route {{ .Method }} {{ .Path }}{{ range .Tags }} {{ . }}{{ end }} {{ camelize .Name }}
{{ if .Summary }}{{ .Summary }}{{ if .Description }}
{{ blockcomment .Description }}{{ end }}{{ else if .Description}}{{ blockcomment .Description }}{{ else }}{{ pascalize .Name }} {{ humanize .Name }} API{{ end }}
*/
type {{ pascalize .Name }} struct {
Context *middleware.Context
Handler {{ pascalize .Name }}Handler
}
func ({{ .ReceiverName }} *{{ pascalize .Name }}) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
route, rCtx, _ := {{ .ReceiverName }}.Context.RouteInfo(r)
if rCtx != nil {
*r = *rCtx
}
var Params = New{{ pascalize .Name }}Params()
{{- if .Authorized }}
uprinc, aCtx, err := {{ .ReceiverName }}.Context.Authorize(r, route)
if err != nil {
{{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err)
return
}
if aCtx != nil {
*r = *aCtx
}
var principal {{ if .PrincipalIsNullable }}*{{ end }}{{ .Principal }}
if uprinc != nil {
principal = {{ if or (eq .Principal "inferface{}") (eq .Principal "any") }}uprinc{{ else }}uprinc.({{ if .PrincipalIsNullable }}*{{ end }}{{ .Principal }}) // this is really a {{ .Principal }}, I promise{{ end }}
}
{{ end }}
if err := {{ .ReceiverName }}.Context.BindValidRequest(r, route, &Params); err != nil { // bind params
{{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err)
return
}
{{ if $.ReturnErrors }}
res, err := {{ .ReceiverName }}.Handler.Handle(Params{{ if .Authorized }}, principal{{ end }}) // actually handle the request
if err != nil {
{{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, err)
return
}
{{ else }}
res := {{ .ReceiverName }}.Handler.Handle(Params{{ if .Authorized }}, principal{{ end }}) // actually handle the request
{{ end }}
{{ .ReceiverName }}.Context.Respond(rw, r, route.Produces, route, res)
}
{{- end }}{{/* TODO(moby): Disabled for moby/api */}}
{{ range .ExtraSchemas }}
// {{ .Name }} {{ comment .Description }}
// {{ .Name }} {{ comment .Description }}{{/* TODO(moby): workaround for "Name" included twice in godoc */}}
//
// swagger:model {{ .Name }}
{{- template "schema" . }}

View File

@@ -1,9 +1,17 @@
{{/*
Custom "structfield" template for Moby; it contains a work around for
CamelCase (IPv6 <--> IPV6) fields.
The orignal template can be found at:
https://github.com/go-swagger/go-swagger/blob/v0.32.3/generator/templates/structfield.gotmpl
*/}}
{{ define "structfield" }}
{{- if not $.IsBaseType -}}
// {{ template "docstring" . }}
{{- template "propertyValidationDocString" .}}
{{- end}}
{{- /* Work around bug where "IPv4" and "IPv6" specifically get mangled to "IPV4" and "IPV6", respectively. */}}
{{- /* TODO(moby): Work around bug where "IPv4" and "IPv6" specifically get mangled to "IPV4" and "IPV6", respectively. */}}
{{- /* {{ pascalize .Name}} {{ template "schemaType" . }} {{ .PrintTags }} */}}
{{ pascalize .Name | replace "IPV4" "IPv4" | replace "IPV6" "IPv6" }} {{ template "schemaType" . }} {{ .PrintTags }}
{{ end }}