Commit 7ba56ef2 authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch 'pl-json-schema-matcher' into 'master'

Improve schema matcher to display errors

See merge request gitlab-org/gitlab!79839
parents c1b01902 c55a8a53
{
"type": "object",
"properties" : {
"properties" : {
"id": { "type": "integer" },
"iid": { "type": "integer" },
......@@ -77,8 +76,9 @@
"additionalProperties": false
},
"assignees": {
"type": "array",
"items": {
"$ref": "./merge_request.json"
"$ref": "user/basic.json"
}
},
"source_project_id": { "type": "integer" },
......@@ -91,21 +91,10 @@
},
"work_in_progress": { "type": "boolean" },
"milestone": {
"type": ["object", "null"],
"properties": {
"id": { "type": "integer" },
"iid": { "type": "integer" },
"project_id": { "type": ["integer", "null"] },
"group_id": { "type": ["integer", "null"] },
"title": { "type": "string" },
"description": { "type": ["string", "null"] },
"state": { "type": "string" },
"created_at": { "type": "string", "format": "date-time" },
"updated_at": { "type": "string", "format": "date-time" },
"due_date": { "type": "string", "format": "date-time" },
"start_date": { "type": "string", "format": "date-time" }
},
"additionalProperties": false
"oneOf": [
{ "type": "null" },
{ "$ref": "milestone.json" }
]
},
"merge_when_pipeline_succeeds": { "type": "boolean" },
"merge_status": { "type": "string" },
......@@ -148,5 +137,4 @@
{ "$ref": "pipeline/detail.json" }
]
}
}
}
......@@ -36,12 +36,38 @@ end
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
match do |response|
schema_path = Pathname.new(SchemaPath.expand(schema, dir))
validator = SchemaPath.validator(schema_path)
@schema_path = Pathname.new(SchemaPath.expand(schema, dir))
validator = SchemaPath.validator(@schema_path)
data = Gitlab::Json.parse(response.body)
@data = Gitlab::Json.parse(response.body)
validator.valid?(data)
@schema_errors = validator.validate(@data)
@schema_errors.none?
end
failure_message do |actual|
message = []
message << <<~MESSAGE
expected JSON response to match schema #{@schema_path.inspect}.
JSON input: #{Gitlab::Json.pretty_generate(@data).indent(2)}
Schema errors:
MESSAGE
@schema_errors.each do |error|
property_name, actual_value = error.values_at('data_pointer', 'data')
property_name = 'root' if property_name.empty?
message << <<~MESSAGE
Property: #{property_name}
Actual value: #{Gitlab::Json.pretty_generate(actual_value).indent(2)}
Error: #{JSONSchemer::Errors.pretty(error)}
MESSAGE
end
message.join("\n")
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment