Skip to content

feat: support for use-httprouter-in-std-http-server in compatibility-options to allow to use github.com/julienschmidt/httprouter#2189

Closed
szarowski wants to merge 27 commits intooapi-codegen:mainfrom
szarowski:feat/httprouter
Closed

feat: support for use-httprouter-in-std-http-server in compatibility-options to allow to use github.com/julienschmidt/httprouter#2189
szarowski wants to merge 27 commits intooapi-codegen:mainfrom
szarowski:feat/httprouter

Conversation

@szarowski
Copy link
Copy Markdown

Adding support for github.com/julienschmidt/httprouter in StdHttpServer

I’d like to ask you for review of PR of the feature to add support for github.com/julienschmidt/httprouter in StdHttpServer.
It is implemented in such way that if a new flag use-httprouter-in-std-http-server in CompatibilityOptions is set to true, the updated code is generated.
The tests and examples are included.
Let me know if the way I trigger is correct and suggest a better approach if any.

Thank you,

Roman

…options to allow to use github.com/julienschmidt/httprouter
@szarowski szarowski requested a review from a team as a code owner January 26, 2026 18:25
Copy link
Copy Markdown
Member

@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

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

Thanks for this - I'd say this should be under output-options instead, after which this looks reasonable

Can we also make sure the JSON Schema is updated?

@szarowski szarowski requested a review from jamietanna January 26, 2026 20:13
@szarowski
Copy link
Copy Markdown
Author

Thanks for this - I'd say this should be under output-options instead, after which this looks reasonable

Can we also make sure the JSON Schema is updated?

Thank you for output-options instead of compatibility. I updated JSON Schema (configuration-schema.json file) and I don't know about any other I should update.. I am probably missing something.. Please be more specific if it is this case.

@szarowski
Copy link
Copy Markdown
Author

Hi, anyone can help reviewing this PR? Does anyone see more issues or have ideas for improvement? @jamietanna Are you ok with the fixed version?

mromaszewicz and others added 19 commits February 10, 2026 20:33
…oapi-codegen#2197)

This is a prototype implementation of a future versions of oapi-codegen. It's almost a full rewrite, heavily
inspired by previous code, and lessons learned.

- much more flexibility in configuring generated code
- move from kin-openapi to libopenapi to support 3.1 and 3.2 specs
- webhook support
- callback support
- incompatible codegen changes to aggregate types (allOf, anyOf, oneOf)
- many existing codegen bugs around schemas fixed
oapi-codegen#2198)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
* Add output filtering

Fixes oapi-codegen#2200

Add output filtering like in V2, and support fetching specs via HTTP.

* Fix lint issue

---------

Co-authored-by: Marcin Romaszewicz <mromaszewicz@nvidia.com>
Co-authored-by: Marcin Romaszewicz <mromaszewicz@nvidia.com>
Thanks to Speakeasy and LivePeer for their support over the years!
As a way to keep updates to the CI pipelines more straightforward, we
can extract this out to a shared, versioned (and updated by Renovate)
Action.

We can make sure to keep our binary builds going, but only for the
current versions of Go - other build failures will be caught by our
shared Action.
…urity] (oapi-codegen#2207)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Yuichiro Tsuji <yui.tsuji@mbk-wellness.co.jp>
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
Co-authored-by: Marcin Romaszewicz <mromaszewicz@users.noreply.github.com>
…api-codegen#1438)

Co-authored-by: Ula <ulad@users.noreply.github.com>
Co-authored-by: Marcin Romaszewicz <mromaszewicz@users.noreply.github.com>
…ers are exist in Iris strict server. (oapi-codegen#1411)

Co-authored-by: Marcin Romaszewicz <mromaszewicz@users.noreply.github.com>
…gnature (oapi-codegen#1419)

* hotfix:
- Bump fiber version to 2.52.0
- Adopted middleware template for fiber to handle new GetReqHeader() method, that has changed signature in fiber 2.50.0 (https://github.com/gofiber/fiber/releases/tag/v2.50.0)

* Use latest fiber, and fix go deps

The latest Fiber requires Go 1.24, therefore, we have to increase
the version in the modules which use it. This is constrained to
tests and examples, so it doesn't affect the main repo.

Go 1.24 can't compile the version of golang.org/x/tools which we
were using, so update that as well.

* fix: use valueList[0] for fiber header IsPassThrough case

The merge with upstream/main resolved a conflict in the fiber
middleware template's header IsPassThrough handler by taking
upstream's version, which still used the old single-string `value`
variable. This is incorrect because fiber 2.50.0+ changed
GetReqHeaders() to return map[string][]string. Fix by using
valueList[0] to match the rest of the header handling block.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Tidy up modules

This seems to be about the minimal set of changes to have
everything build and test cleanly.

* fix: add Go 1.24 version guards to Makefiles

The internal/test and examples modules now require Go 1.24+ in their
go.mod files, but their Makefiles lacked version guards, causing CI
failures on Go 1.22 and 1.23. Add execute-if-go-124 guards matching
the pattern used by other Go 1.24+ modules. Also bump the
strict-server/stdhttp Makefile guard from 1.22 to 1.24.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Alexey Boltunov <apboltunov@mts.ru>
Co-authored-by: Marcin Romaszewicz <marcinr@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
* fixed duplicate type names

* add typename dedup functions

* Fixup: use full-word suffixes and add regression test for issue oapi-codegen#200

- Rename auto-dedup suffixes to use full words: Parameter, Response,
  RequestBody (instead of Param, Resp, ReqBody)
- Add internal/test/issues/issue-200/ with spec, config, generated code,
  and a compile-time regression test that instantiates every expected type

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Gate duplicate type name resolution behind output-options config flag

Add ResolveTypeNameCollisions bool to OutputOptions and the JSON schema.
When false (the default), the codegen errors on duplicate type names as
before. When true, FixDuplicateTypeNames auto-renames colliding types.

Also cleans up ComponentType: removes unused constants, improves doc.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Marcin Romaszewicz <marcinr@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@jamietanna
Copy link
Copy Markdown
Member

Looks like there may have been an issue updating the PR as it's now showing commits from main?

@mromaszewicz what do you think about - similar to #2211 - we make this a string parameter which is the Go import path for a net/http compatible router?

Right now, this would be i.e.

output-options:
  std-http-server-router-override: "github.com/julienschmidt/httprouter"

?

@szarowski
Copy link
Copy Markdown
Author

I synced the latest content from the main branch and rebased to feat/httprouter branch in my fork. Not sure if it caused issues. Should I update it again?

yuro241 and others added 2 commits February 12, 2026 09:56
Co-authored-by: Yuichiro Tsuji <yui.tsuji@mbk-wellness.co.jp>
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
Co-authored-by: Marcin Romaszewicz <mromaszewicz@users.noreply.github.com>
@szarowski
Copy link
Copy Markdown
Author

Looks like there may have been an issue updating the PR as it's now showing commits from main?

@mromaszewicz what do you think about - similar to #2211 - we make this a string parameter which is the Go import path for a net/http compatible router?

Right now, this would be i.e.

output-options:
  std-http-server-router-override: "github.com/julienschmidt/httprouter"

?

@jamietanna What are you going to do with std-http-server-router-override option? I can update it based on the (currently) only specific option value (and the default of course), but it can be more tricky if the other routers are introduced because every router may have different interface and instantiation.

@mromaszewicz
Copy link
Copy Markdown
Member

mromaszewicz commented Feb 12, 2026

I think it's a fine change in principle, but whenever we see an issue like this, I realize that it can affect every router, every import. There could be a reason to override any import, and the question is, do we want to do it globally, or have a per-template config option.

In due time, I'd like to avoid making default decisions at all, and have all the codegen assumptions be data driven, like here:
https://github.com/oapi-codegen/oapi-codegen-exp/blob/main/experimental/Configuration.md

Currently, we gather up the imports we need from the templates in a pretty simple way, I'm thinking that the answer might be to polish that up and create an override map in the config, eg:

import-overrides:
   net/http: http github.com/julienschmidt/httprouter

@mromaszewicz
Copy link
Copy Markdown
Member

hmm, however, that package may not be fully http compatible, so the per-template override makes sense as well.

@szarowski
Copy link
Copy Markdown
Author

hmm, however, that package may not be fully http compatible, so the per-template override makes sense as well.

I think import-overrides are not needed for this feature because github.com/julienschmidt/httprouter package does not override net/http. It is rather additional package, so can be handled as follows:

additional-imports:
  - package: github.com/julienschmidt/httprouter
    alias: httprouter

The only question is how to handle "per-template" solution. If to handle it by a flag
use-httprouter-in-std-http-server: true which I understood you don't like OR implement a tricky condition like:

	if opts.Generate.StdHTTPServer {
		for _, ai := range opts.AdditionalImports {
			if ai.Package == "github.com/julienschmidt/httprouter" {
				stdHTTPServerOut, err = GenerateStdHTTPServerWithHttpRouter(t, ops)
				break
			}
		}
		if stdHTTPServerOut == "" {
			stdHTTPServerOut, err = GenerateStdHTTPServer(t, ops)
		}
		if err != nil {
			return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
		}
	}

What do you recommend?
I am done with the change with this condition and separate templates but I preferred to ask first..

@mromaszewicz
Copy link
Copy Markdown
Member

Would this solve your problem today?

In configuration options, we have this:

output-options:
  user-templates: stdhttp/std-http-handler.tmpl: http-handler-with-julientschmidt.tmpl

You could download the template used and include alongside your config file and simply add the header and import which you want there.

I have a few thoughts here:

  1. I don't think router-specific settings on the output-options are a good long term thing. If we had router specific settings, I think I'd start adding things like StdHttpConfiguration structs, and put that on Configuration, then we pass those through to the relevant templates and they can access those settings directly.

  2. This is a very specific third party package. If we can solve your problem by overriding that one template locally, I don't think we need to make this change for a rare use case.

@szarowski
Copy link
Copy Markdown
Author

Would this solve your problem today?

In configuration options, we have this:

output-options:
  user-templates: stdhttp/std-http-handler.tmpl: http-handler-with-julientschmidt.tmpl

You could download the template used and include alongside your config file and simply add the header and import which you want there.

I have a few thoughts here:

  1. I don't think router-specific settings on the output-options are a good long term thing. If we had router specific settings, I think I'd start adding things like StdHttpConfiguration structs, and put that on Configuration, then we pass those through to the relevant templates and they can access those settings directly.
  2. This is a very specific third party package. If we can solve your problem by overriding that one template locally, I don't think we need to make this change for a rare use case.

I removed output-options completely. and refactored the branch to support the feature with the following configuration:

additional-imports:
  - package: github.com/julienschmidt/httprouter
    alias: httprouter

The condition to select the right template I mentioned is there but the code is more polished, I think.
Tests were added, examples updated.

I also fixed the issues in utils.go and utils_test.go because quite a few tests failed. Let me know if the fixes are OK.

I just found that in go.mod in examples should be correct version of:
require github.com/oapi-codegen/oapi-codegen/v2
but the same issue is in other examples what I see..

@szarowski
Copy link
Copy Markdown
Author

Would this solve your problem today?
In configuration options, we have this:

output-options:
  user-templates: stdhttp/std-http-handler.tmpl: http-handler-with-julientschmidt.tmpl

You could download the template used and include alongside your config file and simply add the header and import which you want there.
I have a few thoughts here:

  1. I don't think router-specific settings on the output-options are a good long term thing. If we had router specific settings, I think I'd start adding things like StdHttpConfiguration structs, and put that on Configuration, then we pass those through to the relevant templates and they can access those settings directly.
  2. This is a very specific third party package. If we can solve your problem by overriding that one template locally, I don't think we need to make this change for a rare use case.

I removed output-options completely. and refactored the branch to support the feature with the following configuration:

additional-imports:
  - package: github.com/julienschmidt/httprouter
    alias: httprouter

The condition to select the right template I mentioned is there but the code is more polished, I think. Tests were added, examples updated.

I also fixed the issues in utils.go and utils_test.go because quite a few tests failed. Let me know if the fixes are OK.

I just found that in go.mod in examples should be correct version of: require github.com/oapi-codegen/oapi-codegen/v2 but the same issue is in other examples what I see..

Actually.. You are right! It works without any changes in the current release !

All I need to do is to configure this:

additional-imports:
  - package: github.com/julienschmidt/httprouter
output-options:
  user-templates:
    stdhttp/std-http-handler.tmpl: templates/http-handler-with-julientschmidt.tmpl

The only hickup is that additional-imports don't work with aliases as in the main branch but it is not important.
I didn't know you support user-templates feature.

Anyway, have a look at my fixes for utils.go and utils_test.go. You have issues there:
34b9d4d
ef6af0c

Thank you !

@szarowski szarowski closed this Feb 13, 2026
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.

9 participants