From a4408ec0e9e13a56fff7ddbfab426061c34caa4c Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Wed, 6 Sep 2023 19:39:07 +0000 Subject: [PATCH] Ensure default floats or ints are converted correctly --- fixtures/number_handling.json | 25 +++++++++++++++++++++++++ reflect.go | 12 ++++++------ reflect_test.go | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 fixtures/number_handling.json diff --git a/fixtures/number_handling.json b/fixtures/number_handling.json new file mode 100644 index 0000000..e65321c --- /dev/null +++ b/fixtures/number_handling.json @@ -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" + ] + } + } +} \ No newline at end of file diff --git a/reflect.go b/reflect.go index 94f4067..c29830f 100644 --- a/reflect.go +++ b/reflect.go @@ -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": @@ -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 { @@ -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) diff --git a/reflect_test.go b/reflect_test.go index e727281..b60bcfd 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -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 @@ -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`) +}