Skip to content

Commit

Permalink
feat: add uint and float support into swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
ispiroglu committed Sep 25, 2024
1 parent 47de00e commit a98f43d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
31 changes: 31 additions & 0 deletions modules/swagger/defs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type testStruct struct {
Field5 *string `json:"field5"`
Field6 map[string]int `json:"field6" validate:"required"`
Field7 string `json:"-"`
Field8 uint32 `json:"field8"`
Field9 float64 `json:"field9"`
}

type anotherStruct struct {
Expand Down Expand Up @@ -119,6 +121,35 @@ func TestBuildModelDefinition(t *testing.T) {
if _, ok := props.(m)["field7"]; ok {
t.Errorf("field7 is not expected in defs as it tagged with '-'")
}

if p, ok := props.(m)["field8"]; !ok {
t.Errorf("Expected field8 in properties, not found")
} else {
if field, ok := p.(m); ok {
if field["type"] != "integer" {
t.Errorf("Expected field8 to be of type int")
}

if field["format"] != "uint32" {
t.Errorf("Expected field8 to be of format uint32")
}
}
}

if p, ok := props.(m)["field9"]; !ok {
t.Errorf("Expected field9 in properties, not found")
} else {
if field, ok := p.(m); ok {
if field["type"] != "number" {
t.Errorf("Expected field9 to be of type number")
}

if field["format"] != "float64" {
t.Errorf("Expected field9 to be of format float64")
}
}
}

} else {

Check failure on line 153 in modules/swagger/defs_test.go

View workflow job for this annotation

GitHub Actions / Lint, Build, and Test (1.23)

unnecessary trailing newline (whitespace)
t.Errorf("Expected properties to be a map, got %T", defs["testStruct"].(m)["properties"])
}
Expand Down
9 changes: 8 additions & 1 deletion modules/swagger/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,20 @@ func withDefinitionPrefix(s string) string {
}

func getPrimitiveType(t reflect.Type) m {
if kp := t.Kind().String(); strings.HasPrefix(kp, "int") {
if kp := t.Kind().String(); strings.HasPrefix(kp, "int") || strings.HasPrefix(kp, "uint") {
return m{
"type": "integer",
"format": kp,
}
}

if kp := t.Kind().String(); strings.HasPrefix(kp, "float") {
return m{
"type": "number",
"format": kp,
}
}

k := t.Kind().String()

if t.Kind() == reflect.Bool {
Expand Down

0 comments on commit a98f43d

Please sign in to comment.