The REST schema inference is a way to create a schema for a service when it is not provided in the service definition or is outdated. This topic describes how you can infer a schema for a web service.
To see how ReadyAPI uses schemas, we use a demo response from the Flickr REST example service. Here is the sample URL:
|The API key often changes, so you may need to go to the Flickr REST demo website to get an up-to-date API key. Open the demo response and copy the
api_key node from it. Then, replace the
api_key parameter in the URL with the new one.
If the request is successful, it returns the following response:
1. Create the Project and Service
First, create a project with the REST service in it:
Select File > New Functional Test.
Click Start under the URL option.
Insert the following URL:https://www.flickr.com/services/rest/?method=flickr.test.echo&format=rest&foo=bar&api_key=494d8532e89c78ae151f23cbacfbf338
The API key often changes, so you may need to go to the Flickr REST demo website to get an up-to-date API key. Open the demo response and copy the
api_keynode from it. Then, replace the
api_keyparameter in the URL with the new one.
ReadyAPI will suggest adding assertions to the test. We won't need them, so you can uncheck them and click Next:
ReadyAPI will offer you to run the test, or create a data-driven test. You do not need either option. Close the dialog.
ReadyAPI creates the project with the Flickr API. You can find it in the Navigator.
2. Get Response
The request is now available in ReadyAPI.
Expand all project items to find the request.
Double-click the request.
ReadyAPI displays the request URL and shows the parameters in the Request section.
The request has all the parameters defined in the URL. You have not sent the request yet, so the Response section is empty.
Click to get the response from the server:
3. Create a Schema
The Schema panel shows the detected schema conflicts:
The analysis of the response shows that there are conflicts between the current response and the previously inferred schema. In this case, this is typical, because no prior schema exists.
To create a schema manually:
Remove the selection from the Auto-Resolve check box.
Click Resolve Conflicts in the Schema panel to resolve the conflicts manually.
For each detected conflict, you will get a notification and can take necessary actions.
We can resolve all conflicts at once, because there is no previous schema. Click Yes to all.
ReadyAPI resolves all conflicts and adds them to the Schema panel.
After ReadyAPI resolves all conflicts, it adds the schema to the Schemas tab:
There is one namespace and the associated XSD schema for it. This schema is based on and can be improved by sending more requests with different parameters. For example, we can issue an invalid request to infer the schema for a fault or make a request for a query that returns an empty result set, and so on.
4. Add More Requests to Schema
To add more definitions to the schema, send a different request. Since we can assume the responses will be valid, you can use the Auto-Resolve option.
Open the Conflicts tab and select the Auto-Resolve option.
To get a different response, remove the
api_keyparameter value from the request:
The response will contain an error:
ReadyAPI resolves and logs errors automatically:
When all conflicts are resolved, the new response will appear in the schema:
Here is the comparison between the resources:
You can keep modifying the request to get more different responses and create a more complex schema.
5. Use the Schema
The main purpose of inferred schemas is to verify that the service response contains no unexpected elements. Use the Schema Compliance assertion for this:
Expand the Assertions panel and click .
Select Compliance, Status and Standards > Schema Compliance and click Add.
ReadyAPI will add the assertion and automatically verify the incoming responses.
|The schema is also displayed in the Specification page of the API editor. Click if the schema is not displayed to reload the definition.