Skip to content

Commit

Permalink
Transfer golang type to swagger type with format reserved (#1944)
Browse files Browse the repository at this point in the history
* transfer golang type to swagger type with format reserved
  • Loading branch information
sdghchj authored Dec 11, 2024
1 parent 9969858 commit 7017870
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 26 deletions.
22 changes: 11 additions & 11 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F

name := matches[1]
paramType := matches[2]
refType := TransToValidSchemeType(matches[3])
refType, format := TransToValidSchemeTypeWithFormat(matches[3])

// Detect refType
objectType := OBJECT

if strings.HasPrefix(refType, "[]") {
objectType = ARRAY
refType = strings.TrimPrefix(refType, "[]")
refType = TransToValidSchemeType(refType)
refType, format = TransToValidSchemeTypeWithFormat(refType)
} else if IsPrimitiveType(refType) ||
paramType == "formData" && refType == "file" {
objectType = PRIMITIVE
Expand All @@ -275,7 +275,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
if objectType == OBJECT {
objectType = PRIMITIVE
}
refType = TransToValidSchemeType(schema.Type[0])
refType, format = TransToValidSchemeTypeWithFormat(schema.Type[0])
enums = schema.Enum
}
}
Expand All @@ -284,7 +284,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
required := requiredText == "true" || requiredText == requiredLabel
description := strings.Join(strings.Split(matches[5], "\\n"), "\n")

param := createParameter(paramType, description, name, objectType, refType, required, enums, operation.parser.collectionFormatInQuery)
param := createParameter(paramType, description, name, objectType, refType, format, required, enums, operation.parser.collectionFormatInQuery)

switch paramType {
case "path", "header", "query", "formData":
Expand Down Expand Up @@ -344,10 +344,10 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
if !IsSimplePrimitiveType(itemSchema.Type[0]) {
continue
}
param = createParameter(paramType, prop.Description, name, prop.Type[0], itemSchema.Type[0], findInSlice(schema.Required, item.Name), itemSchema.Enum, operation.parser.collectionFormatInQuery)
param = createParameter(paramType, prop.Description, name, prop.Type[0], itemSchema.Type[0], format, findInSlice(schema.Required, item.Name), itemSchema.Enum, operation.parser.collectionFormatInQuery)

case IsSimplePrimitiveType(prop.Type[0]):
param = createParameter(paramType, prop.Description, name, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, item.Name), nil, operation.parser.collectionFormatInQuery)
param = createParameter(paramType, prop.Description, name, PRIMITIVE, prop.Type[0], format, findInSlice(schema.Required, item.Name), nil, operation.parser.collectionFormatInQuery)
default:
operation.parser.debug.Printf("skip field [%s] in %s is not supported type for %s", name, refType, paramType)
continue
Expand Down Expand Up @@ -846,9 +846,7 @@ func parseObjectSchema(parser *Parser, refType string, astFile *ast.File) (*spec
case refType == ANY:
return &spec.Schema{}, nil
case IsGolangPrimitiveType(refType):
refType = TransToValidSchemeType(refType)

return PrimitiveSchema(refType), nil
return TransToValidPrimitiveSchema(refType), nil
case IsPrimitiveType(refType):
return PrimitiveSchema(refType), nil
case strings.HasPrefix(refType, "[]"):
Expand Down Expand Up @@ -1183,7 +1181,7 @@ func (operation *Operation) AddResponse(code int, response *spec.Response) {
}

// createParameter returns swagger spec.Parameter for given paramType, description, paramName, schemaType, required.
func createParameter(paramType, description, paramName, objectType, schemaType string, required bool, enums []interface{}, collectionFormat string) spec.Parameter {
func createParameter(paramType, description, paramName, objectType, schemaType string, format string, required bool, enums []interface{}, collectionFormat string) spec.Parameter {
// //five possible parameter types. query, path, body, header, form
result := spec.Parameter{
ParamProps: spec.ParamProps{
Expand All @@ -1207,12 +1205,14 @@ func createParameter(paramType, description, paramName, objectType, schemaType s
Enum: enums,
},
SimpleSchema: spec.SimpleSchema{
Type: schemaType,
Type: schemaType,
Format: format,
},
}
case PRIMITIVE, OBJECT:
result.Type = schemaType
result.Enum = enums
result.Format = format
}
return result
}
Expand Down
1 change: 1 addition & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ func TestParseParamCommentByFormDataTypeUint64(t *testing.T) {
expected := `[
{
"type": "integer",
"format": "int64",
"description": "this is a test file",
"name": "file",
"in": "formData",
Expand Down
4 changes: 2 additions & 2 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (pkgDefs *PackagesDefinitions) parseTypesFromFile(astFile *ast.File, packag
parsedSchemas[typeSpecDef] = &Schema{
PkgPath: typeSpecDef.PkgPath,
Name: astFile.Name.Name,
Schema: PrimitiveSchema(TransToValidSchemeType(idt.Name)),
Schema: TransToValidPrimitiveSchema(idt.Name),
}
}

Expand Down Expand Up @@ -213,7 +213,7 @@ func (pkgDefs *PackagesDefinitions) parseFunctionScopedTypesFromFile(astFile *as
parsedSchemas[typeSpecDef] = &Schema{
PkgPath: typeSpecDef.PkgPath,
Name: astFile.Name.Name,
Schema: PrimitiveSchema(TransToValidSchemeType(idt.Name)),
Schema: TransToValidPrimitiveSchema(idt.Name),
}
}

Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ func (parser *Parser) getTypeSchema(typeName string, file *ast.File, ref bool) (
return &spec.Schema{}, nil
}
if IsGolangPrimitiveType(typeName) {
return PrimitiveSchema(TransToValidSchemeType(typeName)), nil
return TransToValidPrimitiveSchema(typeName), nil
}

schemaType, err := convertFromSpecificToPrimitive(typeName)
Expand Down
9 changes: 6 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,8 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
"type": "integer"
},
"err": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"status": {
"type": "boolean"
Expand Down Expand Up @@ -1788,7 +1789,8 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) {
"type": "integer"
},
"err": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"status": {
"type": "boolean"
Expand Down Expand Up @@ -1947,7 +1949,8 @@ func TestParseStructComment(t *testing.T) {
},
"errorNo": {
"description": "Error ` + "`" + `number` + "`" + ` tick comment",
"type": "integer"
"type": "integer",
"format": "int64"
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,44 @@ func IsNumericType(typeName string) bool {
return typeName == INTEGER || typeName == NUMBER
}

// TransToValidPrimitiveSchema transfer golang basic type to swagger schema with format considered.
func TransToValidPrimitiveSchema(typeName string) *spec.Schema {
switch typeName {
case "int", "uint":
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{INTEGER}}}
case "uint8", "int8", "uint16", "int16", "byte", "int32", "uint32", "rune":
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{INTEGER}, Format: "int32"}}
case "uint64", "int64":
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{INTEGER}, Format: "int64"}}
case "float32", "float64":
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{NUMBER}, Format: typeName}}
case "bool":
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{BOOLEAN}}}
case "string":
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{STRING}}}
}
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{typeName}}}
}

// TransToValidSchemeTypeWithFormat indicates type will transfer golang basic type to swagger supported type with format.
func TransToValidSchemeTypeWithFormat(typeName string) (string, string) {
switch typeName {
case "int", "uint":
return INTEGER, ""
case "uint8", "int8", "uint16", "int16", "byte", "int32", "uint32", "rune":
return INTEGER, "int32"
case "uint64", "int64":
return INTEGER, "int64"
case "float32", "float64":
return NUMBER, typeName
case "bool":
return BOOLEAN, ""
case "string":
return STRING, ""
}
return typeName, ""
}

// TransToValidSchemeType indicates type will transfer golang basic type to swagger supported type.
func TransToValidSchemeType(typeName string) string {
switch typeName {
Expand Down
12 changes: 8 additions & 4 deletions testdata/generics_basic/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,17 @@
"originMapField": {
"type": "object",
"additionalProperties": {
"type": "number"
"type": "number",
"format": "float64"
}
}
}
},
"types.MapField-string-float64": {
"type": "object",
"additionalProperties": {
"type": "number"
"type": "number",
"format": "float64"
}
},
"types.MapFieldNestedStruct-string": {
Expand All @@ -375,7 +377,8 @@
"type": "object",
"properties": {
"f": {
"type": "number"
"type": "number",
"format": "float64"
},
"s": {
"type": "string"
Expand Down Expand Up @@ -431,7 +434,8 @@
},
"errorNo": {
"description": "Error `number` tick comment",
"type": "integer"
"type": "integer",
"format": "int64"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion testdata/generics_names/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@
},
"errorNo": {
"description": "Error `number` tick comment",
"type": "integer"
"type": "integer",
"format": "int64"
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions testdata/generics_property/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
}
},
"items2": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"post": {
"$ref": "#/definitions/types.Field-array_types_Post"
Expand Down Expand Up @@ -444,7 +445,8 @@
}
},
"items2": {
"type": "integer"
"type": "integer",
"format": "int32"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion testdata/state/admin_expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@
"type": "integer"
},
"err": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"status": {
"type": "boolean"
Expand Down
3 changes: 2 additions & 1 deletion testdata/state/user_expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@
"type": "integer"
},
"err": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"status": {
"type": "boolean"
Expand Down

0 comments on commit 7017870

Please sign in to comment.