From 9336a888d4d92833e208ced3fe1745b359d2a8f7 Mon Sep 17 00:00:00 2001 From: extraasterisk Date: Mon, 18 Sep 2023 11:09:18 +0800 Subject: [PATCH 1/3] Limit split for string keywords --- fixtures/equals_in_pattern.json | 25 +++++++++++++++++++++++++ reflect.go | 2 +- reflect_test.go | 6 ++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 fixtures/equals_in_pattern.json diff --git a/fixtures/equals_in_pattern.json b/fixtures/equals_in_pattern.json new file mode 100644 index 0000000..8174c74 --- /dev/null +++ b/fixtures/equals_in_pattern.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/invopop/jsonschema/pattern-equals-test", + "$ref": "#/$defs/PatternEqualsTest", + "$defs": { + "PatternEqualsTest": { + "properties": { + "WithEquals": { + "type": "string", + "pattern": "foo=bar" + }, + "WithEqualsAndCommas": { + "type": "string", + "pattern": "foo,=bar" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "WithEquals", + "WithEqualsAndCommas" + ] + } + } +} \ No newline at end of file diff --git a/reflect.go b/reflect.go index 863b3b7..d7dce99 100644 --- a/reflect.go +++ b/reflect.go @@ -810,7 +810,7 @@ func (t *Schema) booleanKeywords(tags []string) { // read struct tags for string type keyworks func (t *Schema) stringKeywords(tags []string) { for _, tag := range tags { - nameValue := strings.Split(tag, "=") + nameValue := strings.SplitN(tag, "=", 2) if len(nameValue) == 2 { name, val := nameValue[0], nameValue[1] switch name { diff --git a/reflect_test.go b/reflect_test.go index b60bcfd..3083148 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -334,6 +334,11 @@ type Expression struct { Value int `json:"value" jsonschema_extras:"foo=bar=='baz'"` } +type PatternEqualsTest struct { + WithEquals string `jsonschema:"pattern=foo=bar"` + WithEqualsAndCommas string `jsonschema:"pattern=foo\\,=bar"` +} + func TestReflector(t *testing.T) { r := new(Reflector) s := "http://example.com/schema" @@ -466,6 +471,7 @@ func TestSchemaGeneration(t *testing.T) { {ArrayType{}, &Reflector{}, "fixtures/array_type.json"}, {SchemaExtendTest{}, &Reflector{}, "fixtures/custom_type_extend.json"}, {Expression{}, &Reflector{}, "fixtures/schema_with_expression.json"}, + {PatternEqualsTest{}, &Reflector{}, "fixtures/equals_in_pattern.json"}, } for _, tt := range tests { From 8a098cbcb66558b5c55fccd24c7cc42825d906d6 Mon Sep 17 00:00:00 2001 From: extraasterisk Date: Fri, 29 Sep 2023 08:54:08 +0800 Subject: [PATCH 2/3] Fix linter issue --- reflect_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflect_test.go b/reflect_test.go index 3083148..35345d7 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -335,7 +335,7 @@ type Expression struct { } type PatternEqualsTest struct { - WithEquals string `jsonschema:"pattern=foo=bar"` + WithEquals string `jsonschema:"pattern=foo=bar"` WithEqualsAndCommas string `jsonschema:"pattern=foo\\,=bar"` } From 9d905a25553837495e11ecb6d61f5348110dc49f Mon Sep 17 00:00:00 2001 From: extraasterisk Date: Tue, 3 Oct 2023 08:18:51 +0800 Subject: [PATCH 3/3] Limit split on genericKeywords --- reflect.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflect.go b/reflect.go index ad9b56d..b9f35f4 100644 --- a/reflect.go +++ b/reflect.go @@ -681,7 +681,7 @@ func (t *Schema) structKeywordsFromTags(f reflect.StructField, parent *Schema, p func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName string) []string { //nolint:gocyclo unprocessed := make([]string, 0, len(tags)) for _, tag := range tags { - nameValue := strings.Split(tag, "=") + nameValue := strings.SplitN(tag, "=", 2) if len(nameValue) == 2 { name, val := nameValue[0], nameValue[1] switch name {