Skip to content

feat: add SetGlobalStateOptions and ResetGlobalState#2393

Open
toller892 wants to merge 1 commit into
oapi-codegen:mainfrom
toller892:feat/set-global-state-options
Open

feat: add SetGlobalStateOptions and ResetGlobalState#2393
toller892 wants to merge 1 commit into
oapi-codegen:mainfrom
toller892:feat/set-global-state-options

Conversation

@toller892

Copy link
Copy Markdown

Problem

When using oapi-codegen as a library and calling Generate multiple times in a single process (e.g., to split client operations by tags into separate files), the globalState.options from the first call persists and affects subsequent calls. This makes it impossible to generate separate files with different configurations.

Solution

This PR adds two new exported functions, following the pattern established by SetGlobalStateSpec (PR #1174):

  • SetGlobalStateOptions(opts Configuration) — Allows setting globalState.options without calling Generate. Useful when you need to override options between successive Generate calls.

  • ResetGlobalState() — Resets all global state fields to their zero values. This is a more thorough reset that prevents any state leakage between calls, including options, spec, import mappings, type mappings, initialisms, resolved names, and streaming content type regexes.

Usage

// Generate with first set of options
result1, err := codegen.Generate(spec, opts1)

// Reset state before next call
codegen.ResetGlobalState()

// Generate with different options
result2, err := codegen.Generate(spec, opts2)

Or for just overriding options:

codegen.SetGlobalStateOptions(newOpts)
result, err := codegen.Generate(spec, newOpts)

Fixes #1805

Add SetGlobalStateOptions to allow setting globalState.options without
calling Generate, following the pattern established by SetGlobalStateSpec
(PR oapi-codegen#1174).

Add ResetGlobalState to reset all global state fields to their zero
values, preventing state leakage between successive Generate calls when
using oapi-codegen as a library.

Fixes oapi-codegen#1805
@toller892 toller892 requested a review from a team as a code owner June 2, 2026 03:07
@greptile-apps

greptile-apps Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds APIs for controlling codegen's package-level state. The main changes are:

  • Added SetGlobalStateOptions to set stored code generation options directly.
  • Added ResetGlobalState to clear the fields inside globalState.
  • Documented these helpers as support for repeated Generate calls in one process.

Confidence Score: 3/5

This is close, but the reset behavior should be fixed before merging.

  • Repeated Generate calls can still inherit a previous response suffix after ResetGlobalState().

  • Additional initialism handling can also leak through a package-level regex that is not cleared.

  • The changed API is small and the issues are localized to the reset helper.

  • pkg/codegen/codegen.go should reset all option-derived package globals, not only fields inside globalState.

Important Files Changed

Filename Overview
pkg/codegen/codegen.go Adds global state helper APIs, but the reset helper misses option-derived package globals outside globalState.

Reviews (1): Last reviewed commit: "feat: add SetGlobalStateOptions and Rese..." | Re-trigger Greptile

Comment thread pkg/codegen/codegen.go
Comment on lines +1488 to +1497
func ResetGlobalState() {
globalState.options = Configuration{}
globalState.spec = nil
globalState.importMapping = nil
globalState.initialismsMap = nil
globalState.typeMapping = TypeMapping{}
globalState.resolvedNames = nil
globalState.resolvedClientWrapperNames = nil
globalState.streamingContentTypeRegexes = nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Reset misses response suffix

Generate mutates the package-level responseTypeSuffix when OutputOptions.ResponseTypeSuffix is set, but this reset path does not restore it. When a library caller generates once with a custom suffix, calls ResetGlobalState(), and then generates again with default options, the second output still uses the old suffix because Generate only writes responseTypeSuffix for non-empty overrides. That leaves types like GetPetResult in output that should have gone back to GetPetResponse.

Suggested change
func ResetGlobalState() {
globalState.options = Configuration{}
globalState.spec = nil
globalState.importMapping = nil
globalState.initialismsMap = nil
globalState.typeMapping = TypeMapping{}
globalState.resolvedNames = nil
globalState.resolvedClientWrapperNames = nil
globalState.streamingContentTypeRegexes = nil
}
func ResetGlobalState() {
globalState.options = Configuration{}
globalState.spec = nil
globalState.importMapping = nil
globalState.initialismsMap = nil
globalState.typeMapping = TypeMapping{}
globalState.resolvedNames = nil
globalState.resolvedClientWrapperNames = nil
globalState.streamingContentTypeRegexes = nil
responseTypeSuffix = "Response"
}

Comment thread pkg/codegen/codegen.go
Comment on lines +1488 to +1497
func ResetGlobalState() {
globalState.options = Configuration{}
globalState.spec = nil
globalState.importMapping = nil
globalState.initialismsMap = nil
globalState.typeMapping = TypeMapping{}
globalState.resolvedNames = nil
globalState.resolvedClientWrapperNames = nil
globalState.streamingContentTypeRegexes = nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Reset leaves initialism regex

makeInitialismsMap rebuilds the package-level targetWordRegex with any configured AdditionalInitialisms, but ResetGlobalState() only clears the map stored inside globalState. After one generation adds an initialism such as FOO, a reset still leaves ToCamelCaseWithInitialism and paths like media type name generation using a regex that uppercases FOO. A later default generation can therefore still get names influenced by the previous options.

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.

Allow resetting globalState.options

1 participant