You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
stdhttp requires param names to be valid go identifiers #2278
A path can include wildcard segments of the form {NAME} or {NAME...}. For example, "/b/{bucket}/o/{objectname...}". The wildcard name must be a valid Go identifier. Wildcards must be full path segments: they must be preceded by a slash and followed by either a slash or the end of the string. For example, "/b_{bucket}" is not a valid pattern.
I have a case where the openapi definition uses path parameters names with dashes (for example: "addressing-identifier"), which are clearly not valid Go identifiers.
I did not check the other servers but would not be surprised if some were concerned.
Workaround
Rename the parameters in the definition file, which can be cumbersome, especially on imposed definitions that are often updated.
Expected solution
The code generation should rename the parameters when needed.
or
The generation should fail and suggest to switch to gorilla since it doesn't have the issue.
I should mention that the gorilla/mux server seems to handles those parameter names properly, and is an almost drop-in replacement for net/http in this use case. So the right move is probably to switch to gorilla/mux for such cases.
Oh man, I wasn't aware of that restriction. This code is nothing but edge cases.
Normally, I'd suggest using our OpenAPI Overlay functionality to make a "diff" you can apply to that spec whenever you regenerate code, however, OpenAPI Overlay isn't very useful when you try to rename a path - it's built for modifying things under a path element, not modifying path elements.
I think all ServeMux based handlers (stdhttp, chi, gorilla) have this problem, and we'll need to fix all of them by sanitizing the parameter names using our Go identifier sanitizer in those three templates.
Well, gorilla works perfectly on my use case. Aside from the dash, I also had another issue with some args have a prefix. For example "/v1/someapi/code:{code}".
net/http would only accept "/v1/someapi/{code}", ie when a path token contains a parameter, it must be the whole token.
Gorilla does not have this problem either.
Since implementing apis with gorilla & net/http is exactly the same, I think the net/http codegen could just detect the edge cases and fail with an error advising to switch to gorilla.
Yeah, I tested that Chi and Gorilla work, since they implement their own routers.
I'm fixing this for nethttp only. People are picky about their routers. Advising them to switch doesn't always work :) I, for example, am biased towards Echo after years of experience and off-the-shelf middleware use.
I'm not going to worry about the parameters that look like /v1/someapi/code:{code}. These are some old non-standard constructs from a long time ago, and I'd also urge you to avoid them, if you can. You'll see lots of random things fail on this.
Activity
cdevienne commentedon Mar 5, 2026
I should mention that the gorilla/mux server seems to handles those parameter names properly, and is an almost drop-in replacement for net/http in this use case. So the right move is probably to switch to gorilla/mux for such cases.
mromaszewicz commentedon Mar 5, 2026
Oh man, I wasn't aware of that restriction. This code is nothing but edge cases.
Normally, I'd suggest using our OpenAPI Overlay functionality to make a "diff" you can apply to that spec whenever you regenerate code, however, OpenAPI Overlay isn't very useful when you try to rename a path - it's built for modifying things under a path element, not modifying path elements.
I think all ServeMux based handlers (stdhttp, chi, gorilla) have this problem, and we'll need to fix all of them by sanitizing the parameter names using our Go identifier sanitizer in those three templates.
cdevienne commentedon Mar 5, 2026
Well, gorilla works perfectly on my use case. Aside from the dash, I also had another issue with some args have a prefix. For example "/v1/someapi/code:{code}".
net/http would only accept "/v1/someapi/{code}", ie when a path token contains a parameter, it must be the whole token.
Gorilla does not have this problem either.
Since implementing apis with gorilla & net/http is exactly the same, I think the net/http codegen could just detect the edge cases and fail with an error advising to switch to gorilla.
mromaszewicz commentedon Mar 5, 2026
Yeah, I tested that Chi and Gorilla work, since they implement their own routers.
I'm fixing this for nethttp only. People are picky about their routers. Advising them to switch doesn't always work :) I, for example, am biased towards Echo after years of experience and off-the-shelf middleware use.
mromaszewicz commentedon Mar 5, 2026
I'm not going to worry about the parameters that look like
/v1/someapi/code:{code}. These are some old non-standard constructs from a long time ago, and I'd also urge you to avoid them, if you can. You'll see lots of random things fail on this.sanitize param names for http.ServeMux
sanitize param names for http.ServeMux
sanitize param names for http.ServeMux
cdevienne commentedon Mar 6, 2026
I cannot change the api at all, but using Gorilla is perfectly fine for me so it solves both my issues.
sanitize param names for http.ServeMux (#2279)