Skip to content

feat: Use new go-based generator#101

Merged
nderjung merged 3 commits intoprod-stagingfrom
jedevc/improve-openapi
Mar 2, 2026
Merged

feat: Use new go-based generator#101
nderjung merged 3 commits intoprod-stagingfrom
jedevc/improve-openapi

Conversation

@jedevc
Copy link
Collaborator

@jedevc jedevc commented Jan 16, 2026

This removes our dependency on openapitools/openapi-generator-cli, which has a few issues:

  • It's huge. Needing to pull this locally to generate the go code is annoying.
  • The templating language is limited. This work was originally motivated by being unable to inject new functions for doing string casing for enums.

To do this, we adapt all the templates into golang, and do all the parsing in Go. It's a tidy ~1000 lines of Go to do all the parsing. We could do further simplifications as well, we'd need something like getkin/kin-openapi#695 to remove our weird reordering work.

The output is identical. The only difference now is that the generated files get .gen.go filenames, so that they can easily be marked in .gitattributes.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces the dependency on openapitools/openapi-generator-cli with a custom Go-based code generator. The motivation is to eliminate a large dependency and overcome templating language limitations. The output is claimed to be identical, with the only difference being that generated files now use .gen.go filenames for easier identification in .gitattributes.

Changes:

  • Implements a new Go-based OpenAPI code generator (~1000 lines) in tools/codegen/
  • Converts Handlebars templates to Go templates with updated syntax
  • Generates identical output but with .gen.go file extensions
  • Removes dependency on Java-based OpenAPI generator tool

Reviewed changes

Copilot reviewed 37 out of 240 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tools/codegen/*.go New Go-based generator implementation with parser, preprocessor, and template functions
tools/codegen/go.mod Go module dependencies for the new generator
tools/codegen/templates/*.tmpl Converted templates from Handlebars to Go template syntax
templates/*.handlebars Deleted old Handlebars templates
platform/*.gen.go Regenerated model files with new .gen.go extension
platform/.openapi-generator/VERSION Removed as no longer using openapi-generator-cli

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +77 to +80
tmpl, err := template.
New("").
Funcs(templateFuncs{parser}.Funcs()).
ParseFS(templatesFS, "templates/*.tmpl")
Copy link
Member

@nderjung nderjung Jan 16, 2026

Choose a reason for hiding this comment

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

Can you please add the directory to templates as a flag option to the generator itself?

Such that:

- //go:generate go run -C ../tools/codegen . --input ../../controlplane.yaml --output ../../controlplane --package controlplane
+ //go:generate go run -C ../tools/codegen . --input ../../controlplane.yaml --output ../../controlplane --package controlplane --templates ../tools/codegen/templates

I believe @ajpetersons and @petar-cvit would benefit from being able to supply custom templates in separate external projects.

Copy link
Member

Choose a reason for hiding this comment

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

It would also allow us to replace openapi-generator for other languages too actually.

Hmmm. Now I wonder if we should put this in unikraft.com/x/tools...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As discussed offline, this is the eventual aim, so that we can support other SDKs and other codegen from openapi.

However, if we're doing that, the exact functions / data become part of an internal API. I'm not entirely sure I'm happy with the current state being exposed.

We could try and merge this as-is, and then later, we can pull this into x, and make templates fully configurable.

@jedevc jedevc force-pushed the jedevc/improve-openapi branch 3 times, most recently from 3ac0359 to fbf2d5c Compare February 3, 2026 17:04
Signed-off-by: Justin Chadwell <justin@unikraft.com>
@craciunoiuc craciunoiuc self-requested a review March 2, 2026 10:38
jedevc added 2 commits March 2, 2026 11:23
Signed-off-by: Justin Chadwell <justin@unikraft.com>
Signed-off-by: Justin Chadwell <justin@unikraft.com>
@jedevc jedevc force-pushed the jedevc/improve-openapi branch from fbf2d5c to e0a7211 Compare March 2, 2026 11:23
@@ -0,0 +1,50 @@
module unikraft.com/cloud/sdk/tools/codegen

go 1.24.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe bump to the latest version hehe >:D

Copy link
Contributor

@craciunoiuc craciunoiuc left a comment

Choose a reason for hiding this comment

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

I trust this PR with my life

Reviewed-by: Cezar Craciunoiu cezar.craciunoiu@unikraft.com

Copy link
Member

@nderjung nderjung left a comment

Choose a reason for hiding this comment

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

Thanks!

Approved-by: Alexander Jung alex@unikraft.com

@nderjung nderjung merged commit a5bc59b into prod-staging Mar 2, 2026
2 checks passed
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.

4 participants