diff --git a/gen/_template/client.tmpl b/gen/_template/client.tmpl index f9873cd53..98e178995 100644 --- a/gen/_template/client.tmpl +++ b/gen/_template/client.tmpl @@ -220,7 +220,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) send{{ $op.Name }}(ctx c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...)) // Start a span for this request. - ctx, span := c.cfg.Tracer.Start(ctx, {{ quote $op.Name }}, + ctx, span := c.cfg.Tracer.Start(ctx, {{ $op.Name }}Operation, {{- if $hasOTELAttrs }} trace.WithAttributes(otelAttrs...), {{- end }} @@ -283,7 +283,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) send{{ $op.Name }}(ctx { {{- $securityName := $s.Type.Name }} {{ if $otel }}stage = {{ printf "Security:%s" $securityName | quote }}{{ end }} - switch err := c.security{{ $securityName }}(ctx, {{ quote $op.Name }}, r); { + switch err := c.security{{ $securityName }}(ctx, {{ $op.Name }}Operation, r); { case err == nil: // if NO error satisfied[{{ div $idx 8 }}] |= 1 << {{ mod $idx 8 }} case errors.Is(err, ogenerrors.ErrSkipClientSecurity): diff --git a/gen/_template/handlers.tmpl b/gen/_template/handlers.tmpl index 27ab59da1..8bc7797a1 100644 --- a/gen/_template/handlers.tmpl +++ b/gen/_template/handlers.tmpl @@ -47,7 +47,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req {{- end }} // Start a span for this request. - ctx, span := s.cfg.Tracer.Start(r.Context(), {{ quote $op.Name }}, + ctx, span := s.cfg.Tracer.Start(r.Context(), {{ $op.Name }}Operation, {{- if $hasOTELAttrs }} trace.WithAttributes(otelAttrs...), {{- end }} @@ -86,7 +86,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req err error {{- if or $op.Request $op.Params $op.Security.Securities }} opErrContext = ogenerrors.OperationContext{ - Name: {{ quote $op.Name }}, + Name: {{ $op.Name }}Operation, ID: {{ quote $op.Spec.OperationID }}, } {{- end }} @@ -99,7 +99,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req {{- range $idx, $s := $securities }} {{- $securityName := $s.Type.Name }} { - sctx, ok, err := s.security{{ $securityName }}(ctx, {{ quote $op.Name }}, r) + sctx, ok, err := s.security{{ $securityName }}(ctx, {{ $op.Name }}Operation, r) if err != nil { err = &ogenerrors.SecurityError{ OperationContext: opErrContext, @@ -193,7 +193,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req if m := s.cfg.Middleware; m != nil { mreq := middleware.Request{ Context: ctx, - OperationName: {{ quote $op.Name }}, + OperationName: {{ $op.Name }}Operation, OperationSummary: {{ quote $op.Summary }}, OperationID: {{ quote $op.Spec.OperationID }}, Body: {{- if $op.Request }}request{{- else }}nil{{- end }}, diff --git a/gen/_template/operations.tmpl b/gen/_template/operations.tmpl new file mode 100644 index 000000000..8127c2621 --- /dev/null +++ b/gen/_template/operations.tmpl @@ -0,0 +1,17 @@ +{{ define "operations" }} +{{- /*gotype: github.com/ogen-go/ogen/gen.TemplateConfig*/ -}} +{{ template "header" $ }} + +// OperationName is the ogen operation name +type OperationName = string + +const ( + {{- range $op := $.Operations }} + {{ $op.Name }}Operation OperationName = {{ quote $op.Name }} + {{- end }} + {{- range $op := $.Webhooks }} + {{ $op.Name }}Operation OperationName = {{ quote $op.Name }} + {{- end }} +) + +{{ end }} diff --git a/gen/_template/router.tmpl b/gen/_template/router.tmpl index 80b5f81c1..064b2b1c7 100644 --- a/gen/_template/router.tmpl +++ b/gen/_template/router.tmpl @@ -266,7 +266,7 @@ func (s *WebhookServer) Handler(webhookName string) http.Handler { {{- range $route := $r.Routes }} case {{ quote $route.Method }}: {{- $op := $route.Operation }} - r.name = {{ quote $op.Name }} + r.name = {{ $op.Name }}Operation r.summary = {{ quote $op.Summary }} r.operationID = {{ quote $op.Spec.OperationID }} r.pathPattern = {{ quote $route.Path }} diff --git a/gen/_template/security.tmpl b/gen/_template/security.tmpl index cff76adb4..31ceb5e19 100644 --- a/gen/_template/security.tmpl +++ b/gen/_template/security.tmpl @@ -10,9 +10,9 @@ type SecurityHandler interface { // Handle{{ $s.Type.Name }} handles {{ $name }} security. {{- template "godoc" $s.GoDoc }} {{- if $s.Format.IsCustomSecurity }} - Handle{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) (context.Context, error) + Handle{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, error) {{- else }} - Handle{{ $s.Type.Name }}(ctx context.Context, operationName string, t {{ $s.Type.Name }}) (context.Context, error) + Handle{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, t {{ $s.Type.Name }}) (context.Context, error) {{- end }} {{- end }} } @@ -36,7 +36,7 @@ func findAuthorization(h http.Header, prefix string) (string, bool) { {{- if $s.Format.IsOAuth2Security }} var oauth2Scopes{{ $s.Type.Name }} = map[string][]string { {{- range $operationName, $scopes := $s.Scopes }} - {{ quote $operationName }}: []string{ + {{ $operationName }}Operation: []string{ {{- range $scope := $scopes }} {{ quote $scope }}, {{- end}} @@ -45,7 +45,7 @@ var oauth2Scopes{{ $s.Type.Name }} = map[string][]string { } {{- end }} -func (s *Server) security{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) { +func (s *Server) security{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) { {{- if $s.Format.IsAPIKeySecurity }} var t {{ $s.Type.Name }} const parameterName = {{ quote $s.ParameterName }} @@ -124,22 +124,22 @@ type SecuritySource interface { {{- template "godoc" $s.GoDoc }} {{- if $s.Format.IsCustomSecurity }} {{- if $.SecurityReentrantEnabled }} - {{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request, client *Client) error + {{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request, client *Client) error {{- else }} - {{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) error + {{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) error {{- end }} {{- else }} {{- if $.SecurityReentrantEnabled }} - {{ $s.Type.Name }}(ctx context.Context, operationName string, client *Client) ({{ $s.Type.Name }}, error) + {{ $s.Type.Name }}(ctx context.Context, operationName OperationName, client *Client) ({{ $s.Type.Name }}, error) {{- else }} - {{ $s.Type.Name }}(ctx context.Context, operationName string) ({{ $s.Type.Name }}, error) + {{ $s.Type.Name }}(ctx context.Context, operationName OperationName) ({{ $s.Type.Name }}, error) {{- end }} {{- end }} {{- end }} } {{- range $s := $.Securities }} -func (s *Client) security{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) error { +func (s *Client) security{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) error { {{- if $s.Format.IsCustomSecurity }} {{- if $.SecurityReentrantEnabled }} if err := s.sec.{{ $s.Type.Name }}(ctx, operationName, req, s); err != nil { diff --git a/gen/write.go b/gen/write.go index 6e3260b02..b0341f7e2 100644 --- a/gen/write.go +++ b/gen/write.go @@ -332,6 +332,7 @@ func (g *Generator) WriteSource(fs FileSystem, pkgName string) error { {"faker", features.Has(DebugExampleTests)}, {"unimplemented", features.Has(OgenUnimplemented) && genServer}, {"labeler", features.Has(OgenOtel) && genServer}, + {"operations", (genClient || genServer)}, } { t := t if !t.enabled {