Skip to content

fix(stdhttp): reject mixed path-parameter segments at codegen#2492

Open
Solaris-star wants to merge 1 commit into
oapi-codegen:mainfrom
Solaris-star:fix/2488-stdhttp-mixed-path-param
Open

fix(stdhttp): reject mixed path-parameter segments at codegen#2492
Solaris-star wants to merge 1 commit into
oapi-codegen:mainfrom
Solaris-star:fix/2488-stdhttp-mixed-path-param

Conversation

@Solaris-star

Copy link
Copy Markdown

Summary

std-http-server previously generated ServeMux patterns for mixed wildcard+literal segments such as:

/resources/{resourceId}:apply

Registering that with net/http.ServeMux panics:

bad wildcard segment (must end with '}')

Maintainer guidance on #2488 prefers a codegen-time error over silent rewriting.

Fix

  • Add ValidateStdHTTPPath / ValidateStdHTTPPaths
  • Run them from Generate when StdHTTPServer is enabled
  • Reject any path segment that mixes a {param} with surrounding literals

Test plan

  • go test ./pkg/codegen/ -run 'TestValidateStdHTTPPath|TestGenerateRejectsMixedServeMuxPathParam|TestSwaggerUriToStdHttpUriUri'

Fixes #2488

net/http ServeMux requires wildcards to occupy an entire path segment.
Paths like /resources/{resourceId}:apply previously generated patterns
that panic at server registration.

Validate std-http-server paths at codegen time and fail with a clear
error instead of emitting unusable code.

Fixes oapi-codegen#2488
@Solaris-star
Solaris-star requested a review from a team as a code owner July 21, 2026 18:05
@greptile-apps

greptile-apps Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds codegen-time validation for stdhttp ServeMux path patterns. The main changes are:

  • New stdhttp path validators in pkg/codegen/utils.go.
  • A Generate-time check when StdHTTPServer is enabled.
  • Tests for mixed wildcard/literal path segments.

Confidence Score: 4/5

The stdhttp validation path can reject filtered configurations that would not emit the invalid route.

  • The validator itself matches the intended ServeMux constraint.
  • The new Generate check runs over path keys, not only surviving operations.
  • Specs using tag or operation-id filters can fail even when generated output would be valid.

pkg/codegen/codegen.go

Important Files Changed

Filename Overview
pkg/codegen/codegen.go Adds the stdhttp validation call, but it validates path keys that may no longer have emitted operations.
pkg/codegen/utils.go Adds path validation helpers that reject mixed wildcard/literal ServeMux segments.
pkg/codegen/utils_test.go Adds tests for the validator and the main generation rejection path.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
pkg/codegen/codegen.go:181-185
**Filtered Paths Still Fail**

When `std-http-server` is enabled with tag or operation-id filters, those filters can remove every operation under a mixed segment while leaving the path key in `spec.Paths`. This check still validates that unused path and returns an error, so a route that would never be emitted can block generation.

Reviews (1): Last reviewed commit: "fix(stdhttp): reject mixed path-paramete..." | Re-trigger Greptile

Comment thread pkg/codegen/codegen.go
Comment on lines +181 to +185
if opts.Generate.StdHTTPServer {
if err := ValidateStdHTTPPaths(spec); err != nil {
return "", err
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Filtered Paths Still Fail

When std-http-server is enabled with tag or operation-id filters, those filters can remove every operation under a mixed segment while leaving the path key in spec.Paths. This check still validates that unused path and returns an error, so a route that would never be emitted can block generation.

Knowledge Base Used: Codegen Pipeline

Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/codegen/codegen.go
Line: 181-185

Comment:
**Filtered Paths Still Fail**

When `std-http-server` is enabled with tag or operation-id filters, those filters can remove every operation under a mixed segment while leaving the path key in `spec.Paths`. This check still validates that unused path and returns an error, so a route that would never be emitted can block generation.

**Knowledge Base Used:** [Codegen Pipeline](https://app.greptile.com/oapi-codegen/-/custom-context/knowledge-base/oapi-codegen/oapi-codegen/-/docs/codegen-pipeline.md)

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdhttp: mixed path parameter and literal suffix generates invalid ServeMux pattern

1 participant