Skip to content

stdhttp requires param names to be valid go identifiers #2278

Closed

Description

@cdevienne

As quoted from the net/http documentation (https://pkg.go.dev/net/http#hdr-Patterns-ServeMux):

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.

Activity

  1. cdevienne commented on Mar 5, 2026

    @cdevienne
    Author

    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.

  2. mromaszewicz commented on Mar 5, 2026

    @mromaszewicz
    Member

    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.

  3. cdevienne commented on Mar 5, 2026

    @cdevienne
    Author

    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.

  4. mromaszewicz commented on Mar 5, 2026

    @mromaszewicz
    Member

    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.

  5. mromaszewicz commented on Mar 5, 2026

    @mromaszewicz
    Member

    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.

  6. added 3 commits that reference this issue on Mar 5, 2026
    e8cbb61
    cb9c19d
    730c94f
  7. cdevienne commented on Mar 6, 2026

    @cdevienne
    Author

    I cannot change the api at all, but using Gorilla is perfectly fine for me so it solves both my issues.

  8. added a commit that references this issue on Apr 29, 2026
    f0626c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions