-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add SetGlobalStateOptions and ResetGlobalState #2393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generatemutates the package-levelresponseTypeSuffixwhenOutputOptions.ResponseTypeSuffixis set, but this reset path does not restore it. When a library caller generates once with a custom suffix, callsResetGlobalState(), and then generates again with default options, the second output still uses the old suffix becauseGenerateonly writesresponseTypeSuffixfor non-empty overrides. That leaves types likeGetPetResultin output that should have gone back toGetPetResponse.