schemas: Prime oapi-codegen's global state to make capitalization consistent#130
Open
adamwg wants to merge 1 commit into
Open
schemas: Prime oapi-codegen's global state to make capitalization consistent#130adamwg wants to merge 1 commit into
adamwg wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Generatewith 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 newSetGlobalStateOptionsfunction rather than the dummyGeneratecall.Fixes #113
I have:
./nix.sh flake checkto ensure this PR is ready for review.- [ ] Added or updated unit tests.- [ ] Linked a PR or a docs tracking issue to document this change.backport release-x.ylabels to auto-backport this PR.