Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group together all equals, notEquals and oneOf message fields #1352

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions runtime/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ func JSONSchemaForActionResponse(ctx context.Context, schema *proto.Schema, acti
}
}

func contains(s []*proto.MessageField, e string) bool {
for _, input := range s {
if input.Name == e {
return true
}
}

return false
}

// Generates JSONSchema for an operation by generating properties for the root input message.
// Any subsequent nested messages are referenced.
func JSONSchemaForMessage(ctx context.Context, schema *proto.Schema, action *proto.Action, message *proto.Message) JSONSchema {
Expand All @@ -170,7 +180,8 @@ func JSONSchemaForMessage(ctx context.Context, schema *proto.Schema, action *pro

if !isAny {
// Certain messages should only allow one field to be set per request so we set these as a oneOf property
if message.Name == "IdQueryInput" || message.Name == "StringQueryInput" {
fieldsToGroup := (len(message.Fields) == 3 && contains(message.Fields, "equals") && contains(message.Fields, "notEquals") && contains(message.Fields, "oneOf"))
if message.Name == "StringQueryInput" || fieldsToGroup {
oneOf := []JSONSchema{}

for _, field := range message.Fields {
Expand Down Expand Up @@ -202,7 +213,6 @@ func JSONSchemaForMessage(ctx context.Context, schema *proto.Schema, action *pro

root.OneOf = oneOf
root.Type = nil

} else {
for _, field := range message.Fields {
prop := jsonSchemaForField(ctx, schema, action, field.Type, field.Nullable)
Expand Down
4 changes: 3 additions & 1 deletion runtime/jsonschema/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func TestValidateRequest(t *testing.T) {
errors: map[string][]string{
"where.id": {`Must validate one and only one schema (oneOf)`, `Invalid type. Expected: object, given: null`},
"where.title": {`Must validate one and only one schema (oneOf)`, `Invalid type. Expected: object, given: null`},
"where.genre": {`Invalid type. Expected: object, given: null`},
"where.genre": {`Must validate one and only one schema (oneOf)`, `Invalid type. Expected: object, given: null`},
"where.price": {`Invalid type. Expected: object, given: null`},
"where.available": {`Invalid type. Expected: object, given: null`},
"where.createdAt": {`Invalid type. Expected: object, given: null`},
Expand All @@ -752,6 +752,7 @@ func TestValidateRequest(t *testing.T) {
opName: "listBooks",
request: `{"where": {"genre": {"equals": "Sci-fi"}}}`,
errors: map[string][]string{
"where.genre": {`Must validate one and only one schema (oneOf)`},
"where.genre.equals": {`where.genre.equals must be one of the following: "Romance", "Horror", null`},
},
},
Expand All @@ -760,6 +761,7 @@ func TestValidateRequest(t *testing.T) {
opName: "listBooks",
request: `{"where": {"genre": {"oneOf": ["Sci-fi"]}}}`,
errors: map[string][]string{
"where.genre": {`Must validate one and only one schema (oneOf)`},
"where.genre.oneOf.0": {`where.genre.oneOf.0 must be one of the following: "Romance", "Horror"`},
},
},
Expand Down
49 changes: 36 additions & 13 deletions runtime/jsonschema/testdata/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,45 @@
"additionalProperties": false
},
"HobbyQueryInput": {
"type": "object",
"properties": {
"equals": {
"enum": ["Tennis", "Chess", null]
"oneOf": [
{
"additionalProperties": false,
"properties": {
"equals": {
"enum": ["Tennis", "Chess", null]
}
},
"required": ["equals"],
"title": "equals",
"type": "object"
},
"notEquals": {
"enum": ["Tennis", "Chess", null]
{
"additionalProperties": false,
"properties": {
"notEquals": {
"enum": ["Tennis", "Chess", null]
}
},
"required": ["notEquals"],
"title": "notEquals",
"type": "object"
},
"oneOf": {
"type": "array",
"items": {
"enum": ["Tennis", "Chess"]
}
{
"additionalProperties": false,
"properties": {
"oneOf": {
"items": {
"enum": ["Tennis", "Chess"]
},
"type": "array"
}
},
"required": ["oneOf"],
"title": "oneOf",
"type": "object"
}
},
"additionalProperties": false
],
"unevaluatedProperties": false
},
"IdQueryInput": {
"unevaluatedProperties": false,
Expand Down
Loading