Skip to content

Commit

Permalink
Merge pull request #96 from Hotdawg/regex_with_equals
Browse files Browse the repository at this point in the history
Limit split for string keywords
  • Loading branch information
samlown authored Oct 3, 2023
2 parents ab48bdb + 9d905a2 commit cada51d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
25 changes: 25 additions & 0 deletions fixtures/equals_in_pattern.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/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"
]
}
}
}
4 changes: 2 additions & 2 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -804,7 +804,7 @@ func (t *Schema) booleanKeywords(tags []string) {
// read struct tags for string type keywords
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 {
Expand Down
6 changes: 6 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,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"
Expand Down Expand Up @@ -464,6 +469,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 {
Expand Down

0 comments on commit cada51d

Please sign in to comment.