-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Proposal: Create assert_response_schema test helper #1270
Conversation
Wow, this is awesome! I think I made a issue to add this.. specifically for json-api, but in general is also great! I have a few comments on implementation, but am generally wow! |
Hi @bf4 great to hear that 😃 |
module ActiveModel | ||
class Serializer | ||
module Assertions | ||
def assert_response_schema(schema_path = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be under a test namespace if it's for tests, e.g. ActiveModelSerializers::Test::Schema
And would be good for the method not to be dependent on where it's included
e.g.
module Schema
module_function
def validate!(schema_full_path, json, strict = true)
JSON::Validator.validate!(schema_full_path, json, strict: strict) # is this keyword args? we can't support that yet
end
def validate_response_schema!(response, schema_path = nil)
validate!(schema_full_path_for(response.body, schema_path))
end
def schema_full_path_for(response, schema_path = nil)
controller_path = response.request.filtered_parameters[:controller]
action = response.request.filtered_parameters[:action]
schema_directory = ActiveModel::Serializer.config.schema_path
schema_path ||= "#{controller_path}/#{action}.json"
"#{schema_directory}/#{schema_path}"
end
Sorry for delay in review this. There'll be a good rebase ... |
ref: #1162 |
wanna take another look at this? I like this PR |
I think it is better hold the working on this until the #1322 be merged, since both define instrumentation and the last one is more mature. |
instrumentation PR is merged! |
This is a cool idea! I wanted to chime in and share some thoughts about compatibility with other JSON Schema-based tools. Something we discovered recently with It’s likely you’ll run into the same issue with the implementation you have here. If you’re interested in ensuring compatibility with Heroku’s tooling you should consider using |
@malandrina thanks a lot for you thoughts here ❤️ I missed this on README of json-schema is said that this does not handle any JSON Hyper-Schema and prmd uses JSON Hyper-Schema. When you said it makes different assumptions about the organization and structure of the schemata, the recommended way to organize the individual resource schema is under Thanks |
@maurogeorge no problem! Based on my experience with this |
f2d50d1
to
d78179b
Compare
@malandrina I updated to use the With this I think the users can structure their schemata according to Heroku's conventions and storing the schema files in Thanks |
@maurogeorge your process seems solid. 👍 The only other I thing I would suggest thinking about is use of the If you would like for users of the matcher to be able to use JSON Pointers in non-hyper schemata, I'd suggesting writing a test or two to validate your assumptions about how JSON Pointers should be formatted and how If you don't expect users to include JSON Pointers in non-hyper schemata, I'd recommend including that information in the documentation. |
I noticed that the |
@@ -0,0 +1,78 @@ | |||
# How to test | |||
|
|||
## Test helpers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an end-user's guide to testing, right? (vs. in CONTRIBUTING.md how to develop on AMS).
In any case, this is 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bf4 thanks! Yes, this is the idea.
@malandrina thanks for the tip, I updated the code maurogeorge@0852b49 do you think this is enough? Thanks |
@maurogeorge Looks like CI wants you to rebase off of master and force push This PR is so awesome. I love looking at it! |
a3fb472
to
aa9b118
Compare
@bf4 Thanks for the review. I updated the code following your comments. We are back to green after drop Ruby 1.9 on Travis. The AppVeyor is broken because we are running Ruby 1.9.3 on it, I can't found a dot file to change this, I imagine this can be managed via some interface, so we need to change this. Thanks |
@bf4 I updated the code following your last comments and rebased against the master. We are good to ? Thanks |
Looks good but I'm tired right now and want to review more carefully. Did I miss why json_schema is a runtime dependency instead of development? @malandrina thoughts? cc @rails-api/ams this is a good one to look at |
@bf4 thanks, no problem take a rest 😃 I think if we keep the |
Oh right. We could warn inside a method like rails attr_encrypted so rails doesn't need to specify bcrypt B mobile phone
|
|
||
ActiveModelSerializers provides a `assert_response_schema` method to be used on your controller tests to | ||
assert the response against a [JSON Schema](http://json-schema.org/). Let's take | ||
a look in a example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar: "a example" => "an example"
I left some comments with suggested improvements to the documentation but overall this seems reasonable to me. It's great that this will be compatible with Heroku's tooling. 👍 |
@bf4 Liked the idea, I update the code following the same idea Rails does. @malandrina thanks for the review, I updated the docs accordingly. |
@maurogeorge rebase and good to merge, I think. Wanna join the commiters team? |
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
@bf4 rebased, 😊 Yeah, I like the idea to join the commiters team. Probably I will leave the open source world for a while, but later I can continue working on the project. |
@maurogeorge awesome. I'll have @joaomdmoura add you. (I thought I could, but I guess not.) |
@maurogeorge You are invited! ❤️ would be nice to have you on slack so that I can add you on the maintainer channel 😄 |
@bf4 @joaomdmoura thanks ❤️ João as I said in the #1270 (comment), I will leave the open source world for a while, but later I can continue working on the project and help more the project. @bf4 ? |
Merged by ac13053 |
Thanks for the merge 👍 |
It is a common pattern to use JSON Schema to validate a API response 1, 2
and 3.
This patch creates the
assert_response_schema
test helper that helps people dothis kind of validation easily on the controller tests.
This makes sense to you guys? If make sense I can continue with CHANGELOG, Docs and so on.