Skip to content

Commit

Permalink
Merge pull request #93 from invopop/default-number-fix
Browse files Browse the repository at this point in the history
Ensure default floats or ints are converted correctly
  • Loading branch information
samlown committed Sep 6, 2023
2 parents 0ff6727 + a4408ec commit 0d62e11
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
25 changes: 25 additions & 0 deletions fixtures/number_handling.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/invopop/jsonschema/number-handler",
"$ref": "#/$defs/NumberHandler",
"$defs": {
"NumberHandler": {
"properties": {
"int64": {
"type": "integer",
"default": 12
},
"float32": {
"type": "number",
"default": 12.5
}
},
"additionalProperties": false,
"type": "object",
"required": [
"int64",
"float32"
]
}
}
}
12 changes: 6 additions & 6 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,9 @@ func (t *Schema) structKeywordsFromTags(f reflect.StructField, parent *Schema, p
case "string":
t.stringKeywords(tags)
case "number":
t.numbericKeywords(tags)
t.numericalKeywords(tags)
case "integer":
t.numbericKeywords(tags)
t.numericalKeywords(tags)
case "array":
t.arrayKeywords(tags)
case "boolean":
Expand Down Expand Up @@ -844,8 +844,8 @@ func (t *Schema) stringKeywords(tags []string) {
}
}

// read struct tags for numberic type keyworks
func (t *Schema) numbericKeywords(tags []string) {
// read struct tags for numerical type keyworks
func (t *Schema) numericalKeywords(tags []string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
Expand All @@ -867,8 +867,8 @@ func (t *Schema) numbericKeywords(tags []string) {
b, _ := strconv.ParseBool(val)
t.ExclusiveMinimum = b
case "default":
i, _ := strconv.Atoi(val)
t.Default = i
n, _ := strconv.ParseFloat(val, 64)
t.Default = n
case "example":
if i, err := strconv.Atoi(val); err == nil {
t.Examples = append(t.Examples, i)
Expand Down
19 changes: 19 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ func compareSchemaOutput(t *testing.T, f string, r *Reflector, obj interface{})
}
}

func fixtureContains(t *testing.T, f, s string) {
t.Helper()
b, err := os.ReadFile(f)
require.NoError(t, err)
assert.Contains(t, string(b), s)
}

func TestSplitOnUnescapedCommas(t *testing.T) {
tests := []struct {
strToSplit string
Expand Down Expand Up @@ -572,3 +579,15 @@ func TestFieldOneOfRef(t *testing.T) {
r := &Reflector{}
compareSchemaOutput(t, "fixtures/oneof_ref.json", r, &Server{})
}

func TestNumberHandling(t *testing.T) {
type NumberHandler struct {
Int64 int64 `json:"int64" jsonschema:"default=12"`
Float32 float32 `json:"float32" jsonschema:"default=12.5"`
}

r := &Reflector{}
compareSchemaOutput(t, "fixtures/number_handling.json", r, &NumberHandler{})
fixtureContains(t, "fixtures/number_handling.json", `"default": 12`)
fixtureContains(t, "fixtures/number_handling.json", `"default": 12.5`)
}

0 comments on commit 0d62e11

Please sign in to comment.