Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 6 additions & 8 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ var templates embed.FS
// globalState stores all global state. Please don't put global state anywhere
// else so that we can easily track it.
var globalState struct {
options Configuration
spec *openapi3.T
options Configuration
spec *openapi3.T
importMapping importMap
}

// goImport represents a go package to be imported in the generated code
Expand Down Expand Up @@ -67,8 +68,6 @@ func (im importMap) GoImports() []string {
return goImports
}

var importMapping importMap

func constructImportMapping(importMapping map[string]string) importMap {
var (
pathToName = map[string]string{}
Expand Down Expand Up @@ -101,8 +100,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
// This is global state
globalState.options = opts
globalState.spec = spec

importMapping = constructImportMapping(opts.ImportMapping)
globalState.importMapping = constructImportMapping(opts.ImportMapping)

filterOperationsByTag(spec, opts)
if !opts.OutputOptions.SkipPrune {
Expand Down Expand Up @@ -234,7 +232,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {

var inlinedSpec string
if opts.Generate.EmbeddedSpec {
inlinedSpec, err = GenerateInlinedSpec(t, importMapping, spec)
inlinedSpec, err = GenerateInlinedSpec(t, globalState.importMapping, spec)
if err != nil {
return "", fmt.Errorf("error generating Go handlers for Paths: %w", err)
}
Expand All @@ -243,7 +241,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
var buf bytes.Buffer
w := bufio.NewWriter(&buf)

externalImports := append(importMapping.GoImports(), importMap(xGoTypeImports).GoImports()...)
externalImports := append(globalState.importMapping.GoImports(), importMap(xGoTypeImports).GoImports()...)
importsOut, err := GenerateImports(t, externalImports, opts.PackageName)
if err != nil {
return "", fmt.Errorf("error generating imports: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func refPathToGoType(refPath string, local bool) (string, error) {
return "", fmt.Errorf("unsupported reference: %s", refPath)
}
remoteComponent, flatComponent := pathParts[0], pathParts[1]
if goImport, ok := importMapping[remoteComponent]; !ok {
if goImport, ok := globalState.importMapping[remoteComponent]; !ok {
return "", fmt.Errorf("unrecognized external reference '%s'; please provide the known import for this reference using option --import-mapping", remoteComponent)
} else {
goType, err := refPathToGoType("#"+flatComponent, false)
Expand Down
6 changes: 3 additions & 3 deletions pkg/codegen/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ func TestSortedRequestBodyKeys(t *testing.T) {
}

func TestRefPathToGoType(t *testing.T) {
old := importMapping
importMapping = constructImportMapping(map[string]string{
old := globalState.importMapping
globalState.importMapping = constructImportMapping(map[string]string{
"doc.json": "externalref0",
"http://deepmap.com/doc.json": "externalref1",
})
defer func() { importMapping = old }()
defer func() { globalState.importMapping = old }()

tests := []struct {
name string
Expand Down