Skip to content

Commit

Permalink
Fix types starting with numbers
Browse files Browse the repository at this point in the history
Prefix type and variable names for schemas that start with
numbers with an 'N'
  • Loading branch information
Marcin Romaszewicz committed Jul 27, 2019
1 parent 578223b commit 2177b2d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 23 deletions.
130 changes: 121 additions & 9 deletions internal/test/schemas/schemas.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/test/schemas/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ paths:
required: true
schema:
type: string
/issues/41/{1param}:
get:
operationId: Issue41
description: Parameter name starting with number
parameters:
- name: 1param
in: path
required: true
schema:
$ref: "#/components/schemas/5StartsWithNumber"
components:
schemas:
GenericObject:
Expand All @@ -47,3 +57,6 @@ components:
CustomStringType:
type: string
format: custom
5StartsWithNumber:
type: object
description: This schema name starts with a number
14 changes: 7 additions & 7 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3.

types = append(types, TypeDefinition{
JsonName: schemaName,
TypeName: ToCamelCase(schemaName),
TypeName: SchemaNameToTypeName(schemaName),
Schema: goSchema,
})

Expand All @@ -303,7 +303,7 @@ func GenerateTypesForParameters(t *template.Template, params map[string]*openapi
typeDef := TypeDefinition{
JsonName: paramName,
Schema: goType,
TypeName: ToCamelCase(paramName),
TypeName: SchemaNameToTypeName(paramName),
}

if paramOrRef.Ref != "" {
Expand All @@ -312,7 +312,7 @@ func GenerateTypesForParameters(t *template.Template, params map[string]*openapi
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error generating Go type for (%s) in parameter %s", paramOrRef.Ref, paramName))
}
typeDef.TypeName = ToCamelCase(refType)
typeDef.TypeName = SchemaNameToTypeName(refType)
}

types = append(types, typeDef)
Expand Down Expand Up @@ -342,7 +342,7 @@ func GenerateTypesForResponses(t *template.Template, responses openapi3.Response
typeDef := TypeDefinition{
JsonName: responseName,
Schema: goType,
TypeName: ToCamelCase(responseName),
TypeName: SchemaNameToTypeName(responseName),
}

if responseOrRef.Ref != "" {
Expand All @@ -351,7 +351,7 @@ func GenerateTypesForResponses(t *template.Template, responses openapi3.Response
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error generating Go type for (%s) in parameter %s", responseOrRef.Ref, responseName))
}
typeDef.TypeName = ToCamelCase(refType)
typeDef.TypeName = SchemaNameToTypeName(refType)
}
types = append(types, typeDef)
}
Expand Down Expand Up @@ -380,7 +380,7 @@ func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*open
typeDef := TypeDefinition{
JsonName: bodyName,
Schema: goType,
TypeName: ToCamelCase(bodyName),
TypeName: SchemaNameToTypeName(bodyName),
}

if bodyOrRef.Ref != "" {
Expand All @@ -389,7 +389,7 @@ func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*open
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error generating Go type for (%s) in body %s", bodyOrRef.Ref, bodyName))
}
typeDef.TypeName = ToCamelCase(refType)
typeDef.TypeName = SchemaNameToTypeName(refType)
}
types = append(types, typeDef)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"
"text/template"
"unicode"

"github.com/getkin/kin-openapi/openapi3"
"github.com/pkg/errors"
Expand Down Expand Up @@ -112,6 +113,9 @@ func (pd ParameterDefinition) GoVariableName() string {
if IsGoKeyword(name) {
name = "p" + UppercaseFirstCharacter(name)
}
if unicode.IsNumber([]rune(name)[0]) {
name = "n" + name
}
return name
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Property struct {
}

func (p Property) GoFieldName() string {
return ToCamelCase(p.JsonFieldName)
return SchemaNameToTypeName(p.JsonFieldName)
}

func (p Property) GoTypeDef() string {
Expand Down
15 changes: 9 additions & 6 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func RefPathToGoType(refPath string) (string, error) {
if len(pathParts) != 4 {
return "", errors.New("Parameter nesting is deeper than supported")
}
return ToCamelCase(pathParts[3]), nil
return SchemaNameToTypeName(pathParts[3]), nil
}

// This function converts a swagger style path URI with parameters to a
Expand Down Expand Up @@ -293,12 +293,15 @@ func IsGoKeyword(str string) bool {
return false
}

// Prefixes a string if it's a go keyword
func PrefixKeyword(str, prefix string) string {
if IsGoKeyword(str) {
return prefix + UppercaseFirstCharacter(str)
// Converts a Schema name to a valid Go type name. It converts to camel case, and makes sure the name is
// valid in Go
func SchemaNameToTypeName(name string) string {
name = ToCamelCase(name)
// Prepend "N" to schemas starting with a number
if unicode.IsDigit([]rune(name)[0]) {
name = "N" + name
}
return str
return name
}

// According to the spec, additionalProperties may be true, false, or a
Expand Down

0 comments on commit 2177b2d

Please sign in to comment.