Skip to content
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

Fix possible empty values in ReadBuildInfo() #548

Merged
merged 1 commit into from Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions pkg/codegen/codegen.go
Expand Up @@ -121,7 +121,6 @@ func Generate(swagger *openapi3.T, packageName string, opts Options) (string, er
// This parses all of our own template files into the template object
// above
err := LoadTemplates(templates, t)

if err != nil {
return "", fmt.Errorf("error parsing oapi-codegen templates: %w", err)
}
Expand Down Expand Up @@ -225,7 +224,6 @@ func Generate(swagger *openapi3.T, packageName string, opts Options) (string, er
_, err = w.WriteString(typeDefinitions)
if err != nil {
return "", fmt.Errorf("error writing type definitions: %w", err)

}

if opts.GenerateClient {
Expand Down Expand Up @@ -366,7 +364,7 @@ func GenerateConstants(t *template.Template, ops []OperationDefinition) (string,
// Generates type definitions for any custom types defined in the
// components/schemas section of the Swagger spec.
func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3.SchemaRef, excludeSchemas []string) ([]TypeDefinition, error) {
var excludeSchemasMap = make(map[string]bool)
excludeSchemasMap := make(map[string]bool)
for _, schema := range excludeSchemas {
excludeSchemasMap[schema] = true
}
Expand Down Expand Up @@ -556,22 +554,23 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)
}

return GenerateTemplates([]string{"constants.tmpl"}, t, c)

}

// Generate our import statements and package definition.
func GenerateImports(t *template.Template, externalImports []string, packageName string) (string, error) {
// Read build version for incorporating into generated files
var modulePath string
var moduleVersion string
// Unit tests have ok=false, so we'll just use "unknown" for the
// version if we can't read this.

modulePath := "unknown module path"
moduleVersion := "unknown version"
if bi, ok := debug.ReadBuildInfo(); ok {
modulePath = bi.Main.Path
moduleVersion = bi.Main.Version
} else {
// Unit tests have ok=false, so we'll just use "unknown" for the
// version if we can't read this.
modulePath = "unknown module path"
moduleVersion = "unknown version"
if bi.Main.Path != "" {
modulePath = bi.Main.Path
}
if bi.Main.Version != "" {
moduleVersion = bi.Main.Version
}
}

context := struct {
Expand Down Expand Up @@ -615,7 +614,6 @@ func GenerateAdditionalPropertyBoilerplate(t *template.Template, typeDefs []Type
}

return GenerateTemplates([]string{"additional-properties.tmpl"}, t, context)

}

// SanitizeCode runs sanitizers across the generated Go code to ensure the
Expand Down