Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,3 +1474,24 @@ func GetParametersImports(params map[string]*openapi3.ParameterRef) (map[string]
func SetGlobalStateSpec(spec *openapi3.T) {
globalState.spec = spec
}

// SetGlobalStateOptions allows setting globalState.options without calling
// Generate. This is useful when using oapi-codegen as a library and calling
// Generate multiple times with different options in a single process.
func SetGlobalStateOptions(opts Configuration) {
globalState.options = opts
}

// ResetGlobalState resets all global state to its zero value. This is useful
// when using oapi-codegen as a library and calling Generate multiple times
// in a single process to ensure no state leaks between calls.
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
}
Comment on lines +1488 to +1497

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 on lines +1488 to +1497

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.