Continuing the discussion from How do you test your JSON APIs?5:
Warning: this is a bit of a braindump only lightly edited
I've been trying all sorts of things and have yet to find a sane solution.
Here's the problem:
- I want to build an app that services an api.
- I want to be able to make it easy to test by humans, e.g. via swagger6 (does anyone use jsonpath6 to test json resources?)
- I want to be able to easily generate a client for it e.g. the heroku api toolchain 4
- I want to be able to validate that the client and service agree on what the api is, and ideally change in sync
- I don't want to define the api in the code, as that makes it harder to work with. e.g. if I define the api in a controller, how do I create the resource serializers and the client? I also just don't want to be stuck with a library that makes it harder to change
There are tons of tools out there that seem to do parts of this, but ultimately they either
- derive the contracts from the service, which makes testing the service a little weird
- expect you to know how to write define your json-schema1 (and know what that is) in apiblueprint2, swagger, raml1, whatever format from scratch
As https://brandur.org/elegant-apis3 writes
To recap, we’ve used JSON Schema to define the following:
- Individual API resources (app and domain).
- An API “super schema” that contains all resources in a single document.
- Hyper-schema links that describe actions on those resources.
- Schemas that validate incoming requests on each link.
- Schemas that describe the JSON returned by each link.
- A meta-schema that validates the conventions of our API’s schema.
Although the API itself still needs to be implemented, by combining this schema with the various packages from the HTTP toolchain, we get some nice features for free:
- Generate API documentation with Prmd2.
- Generate a Ruby client with Heroics4.
- Generate a Go client with Schematic, like the one used in Heroku’s new CLI, hk1.
- Boot a working stub with Committee2 that will validate incoming requests.
- Insert a request validation middleware with Committee that will validate incoming request data according to schema before it reaches our stack.
- Use Committee’s test helpers to verify that the responses from our stack conform to the schema.
The most straightforward solution to this seems to be 'documentation-driven contracts' (or feature specs), where the contract
- is a valid schema e.g. https://github.com/ruby-json-schema/json-schema https://github.com/patchboard/patchboard2
- is used to validate the client
- is used to validate the service
- is used to validate requests e.g. https://github.com/interagent/committee2
but this requires I understand how to write a json-schema and probably a hypermedia type, and how to propogate changes in a non-breaking way (versioned microservices!)
and my head just keeps exploding and I never get it the way I want it. But I know people are doing this...
A simple stupid way is the document the request/responses I want and then use https://github.com/maxlinc/json-schema-generator1 to generate a json schema from it, and then tweak that... or https://github.com/apiaryio/curl-trace-parser will let me turn a curl request to my service into an apiblueprint doc, which.. idk
I really like using swagger, but a lot of tools still use 1.2 which isn't "json-schema", and when I've tried converting a 1.2 yml to a 2.0 with https://github.com/apigee-127/swagger-tools I've had validation failures up the wazoo even though the public/api page works fine...
best I can figure right now is to take some examples and just cannibalize them.
And the appendix is then which tools do I use that don't lock me in. If I use grape and roar with hal or rails with active_model_resources or with https://github.com/RestPack/restpack_serializer and then I decide I want all resource keys to always be plural and return a collection, and there's always a meta key that we use for pagination (not in the header for whatever reason), the tooling can really slow me down... and will jsonapi1 save the day? not yet
thoughts?
http://parley.rubyrogues.com/t/how-do-you-test-your-json-apis/2759/6
Links:
http://parley.rubyrogues.com/t/json-apis-schema-validation-document-driven-consumer-driven-media-type-oh-my/3177
Continuing the discussion from How do you test your JSON APIs?5:
Warning: this is a bit of a braindump only lightly edited
I've been trying all sorts of things and have yet to find a sane solution.
Here's the problem:
There are tons of tools out there that seem to do parts of this, but ultimately they either
- derive the contracts from the service, which makes testing the service a little weird
- expect you to know how to write define your json-schema1 (and know what that is) in apiblueprint2, swagger, raml1, whatever format from scratch
As https://brandur.org/elegant-apis3 writes
The most straightforward solution to this seems to be 'documentation-driven contracts' (or feature specs), where the contract
- is a valid schema e.g. https://github.com/ruby-json-schema/json-schema https://github.com/patchboard/patchboard2
- is used to validate the client
- is used to validate the service
- is used to validate requests e.g. https://github.com/interagent/committee2
but this requires I understand how to write a json-schema and probably a hypermedia type, and how to propogate changes in a non-breaking way (versioned microservices!)
and my head just keeps exploding and I never get it the way I want it. But I know people are doing this...
A simple stupid way is the document the request/responses I want and then use https://github.com/maxlinc/json-schema-generator1 to generate a json schema from it, and then tweak that... or https://github.com/apiaryio/curl-trace-parser will let me turn a curl request to my service into an apiblueprint doc, which.. idk
I really like using swagger, but a lot of tools still use 1.2 which isn't "json-schema", and when I've tried converting a 1.2 yml to a 2.0 with https://github.com/apigee-127/swagger-tools I've had validation failures up the wazoo even though the public/api page works fine...
best I can figure right now is to take some examples and just cannibalize them.
And the appendix is then which tools do I use that don't lock me in. If I use grape and roar with hal or rails with active_model_resources or with https://github.com/RestPack/restpack_serializer and then I decide I want all resource keys to always be plural and return a collection, and there's always a meta key that we use for pagination (not in the header for whatever reason), the tooling can really slow me down... and will jsonapi1 save the day? not yet
thoughts?