Skip to content

schemas: Prime oapi-codegen's global state to make capitalization consistent#130

Open
adamwg wants to merge 1 commit into
crossplane:mainfrom
adamwg:awg/initialize-codegen
Open

schemas: Prime oapi-codegen's global state to make capitalization consistent#130
adamwg wants to merge 1 commit into
crossplane:mainfrom
adamwg:awg/initialize-codegen

Conversation

@adamwg

@adamwg adamwg commented Jun 18, 2026

Copy link
Copy Markdown
Member

Description of your changes

The oapi-codegen package we use to generate Go code keeps a bunch of internal global state that affects how code is generated. In particular, the name normalizer it uses to form types names is a global variable initialized when we call codegen.Generate.

We call some of the codegen package's utility functions to form custom names for types before generating code. These utility functions also use the global state set up by codegen.Generate, which isn't initialized when we call them, resulting in the utility functions producing different names than we expect. This makes our code generation non-deterministic: we get different name capitalization depending what order schemas get generated in.

Make a no-op call to codegen.Generate with our desired options before calling the mutators to ensure that the global state is initialized for all our calls. Note that once oapi-codegen/oapi-codegen#2393 is merged in the upstream code generator, we can use the new SetGlobalStateOptions function rather than the dummy Generate call.

Fixes #113

I have:

…sistent

The oapi-codegen package we use to generate Go code keeps a bunch of internal
global state that affects how code is generated. In particular, the name
normalizer it uses to form types names is a global variable initialized when we
call `codegen.Generate`.

We call some of the codegen package's utility functions to form custom names for
types before generating code. These utility functions also use the global state
set up by `codegen.Generate`, which isn't initialized when we call them,
resulting in the utility functions producing different names than we
expect. This makes our code generation non-deterministic: we get different name
capitalization depending what order schemas get generated in.

Make a no-op call to `codegen.Generate` with our desired options before calling
the mutators to ensure that the global state is initialized for all our
calls. Note that once oapi-codegen/oapi-codegen#2393 is
merged in the upstream code generator, we can use the new
`SetGlobalStateOptions` function rather than the dummy `Generate` call.

Fixes crossplane#113

Signed-off-by: Adam Wolfe Gordon <awg@upbound.io>
@adamwg adamwg requested a review from a team as a code owner June 18, 2026 17:16
@adamwg adamwg requested review from phisco and removed request for a team June 18, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated Go schemas use inconsistent capitalization

1 participant