Inferring REST Schemas

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.

Sample Request

To see how ReadyAPI uses schemas, we use a demo response from the Flickr REST example service. Here is the sample URL:

go.gifhttps://www.flickr.com/services/rest/?method=flickr.test.echo&format=rest&foo=bar&api_key=494d8532e89c78ae151f23cbacfbf338.

Important

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.

API key

If the request is successful, it returns the following response:

<rsp stat="ok">
    <method>flickr.test.echo</method>
    <format>rest</format>
    <foo>bar</foo>
    <api_key>494d8532e89c78ae151f23cbacfbf338</api_key>
</rsp>

1. Create the Project and Service

First, create a project with the REST service in it:

  1. Select File > New Functional Test.

    New Functional Test
  2. Click Start under the URL option.

    Choosing URL
  3. Insert the following URL:

    https://www.flickr.com/services/rest/?method=flickr.test.echo&format=rest&foo=bar&api_key=494d8532e89c78ae151f23cbacfbf338

    Important

    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.

    Click Next.

    Entering the URL
  4. ReadyAPI will suggest adding assertions to the test. We won't need them, so you can uncheck them and click Next:

    Unchecking assertions
  5. ReadyAPI will offer you to run the test or create a data-driven test. You do not need either option. Close the dialog.

    Closing the confirmation dialog

ReadyAPI creates the project with the Flickr API. You can find it in the Navigator.

The new project

2. Get Response

The request is now available in ReadyAPI.

  1. Expand all project items to find the request.

    Request
  2. Double-click the request.

    ReadyAPI displays the request URL and shows the parameters in the Request section.

    The request section
  3. The request has all the parameters defined in the URL. You have not sent the request yet, so the Response section is empty.

    Blank response
  4. Click run.png to get the response from the server:

    The Response section

3. Create a Schema

The Schema panel shows the detected schema conflicts:

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.

    Clear Auto-Resolve
  • Click Resolve Conflicts in the Schema panel to resolve the conflicts manually.

    Clicking Resolve conflicts
  • For each detected conflict, you will get a notification and can take necessary actions.

    Conflict warning
  • 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.

    Resolved conflicts

After ReadyAPI resolves all conflicts, it adds the schema to the Schemas tab:

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.

    Auto-Resolve selected
  • To get a different response, remove the api_key parameter value from the request:

    Removing the api_key
  • Click run.png.

  • The response will contain an error:

    Response error
  • ReadyAPI resolves and logs errors automatically:

    Automatically resolved conflicts
  • When all conflicts are resolved, the new response will appear in the schema:

    New schema

    Here is the comparison between the resources:

    Before

    After

    Resource before resolving
    Resource after resolving

    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 add.png.

    Adding Schema Compliance assertions
  • Select Compliance, Status and Standards > Schema Compliance and click Add.

ReadyAPI will add the assertion and automatically verify the incoming responses.

Tip

The schema is also displayed in the Specification page of the API editor. Click Reload if the schema is not displayed to reload the definition.

See Also

Publication date: