Inferring REST Schemas

REST schema inference creates a schema for a service when the service definition does not provide one or when it is outdated. This topic describes how to infer a schema for a web service.

Sample Request

To demonstrate how ReadyAPI uses schemas, this topic uses a demo response from the Flickr REST example service. Here is the sample URL:

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

Important

The API key changes frequently. Go to the Flickr REST demo website to get an up-to-date API key. Open the demo response, copy the api_key node value, then replace the api_key parameter in the URL with the new value.

If the request succeeds, it returns the following response:

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

1. Create the Project and Service

Create a project with the REST service:

  1. From the main menu, select File and then New Functional Test.

  2. In the New Functional Test dialog, click Endpoint.

    The Endpoint Setup flow guides you through several steps.

  3. The Endpoint Setup flow guides you through several steps. Confirm that GET is selected in the Type dropdown.

  4. Enter the following API URL in the URL field:

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

    Important

    The API key changes frequently. Go to the Flickr REST demo website to get an up-to-date API key. Open the demo response, copy the api_key node value, then replace the api_key parameter in the URL with the new value.

    ReadyAPI New Functional Test wizard showing the Endpoint Setup step. The Enter URL step is active in the four-step progress indicator. A GET request type is selected with the Flickr REST API URL entered in the URL field, with the api_key parameter value t
  5. Click Next.

  6. In the Specify Project step, select where to add the test:

    • Select <New project> to create a new project

    • Or select an existing project

    Click Next.

    Note

    If no project is open in your workspace, ReadyAPI creates a new project automatically.

  7. In the Add Assertions step, deselect all selected assertions, then click Next.

    ReadyAPI New Functional Test wizard showing the Add Assertions step active in the four-step progress indicator, with Enter URL and Specify Project steps completed. Two assertion options are available: Valid HTTP Status Codes and Response SLA Assertion, bo
  8. In the Confirmation dialog, choose one of the following:

    • Run Test Suite to run the generated test cases

    • Add a Data Source to add external data (for example, from a file)

    For this tutorial, close the dialog.

    Your test project and test cases are ready. Find them in the Navigator panel.

    ReadyAPI Navigator panel showing the workspace structure with REST Project 2 expanded. Under Functional Tests, the https://www.flickr.com Test Suite contains https://www.flickr.com Test Case 1 with Request 1 selected. Security Tests, Performance Tests, an

2. Get Response

The request is now available to evaluate.

  1. In the Navigator, click Request 1 to open it in the request editor.

    ReadyAPI Navigator panel showing REST Project 2 expanded with Request 1 highlighted and outlined in orange under https://www.flickr.com Test Case 1, within the https://www.flickr.com Test Suite under Functional Tests. Security Tests, Performance Tests, an
  2. In the request editor, verify that the METHOD is set to GET and the ENDPOINT is set to https://www.flickr.com with the RESOURCE /services/rest/. In the Request panel, confirm the following query parameters are populated:

    ReadyAPI Test Step Request 1 editor showing a GET request to the /services/rest/ resource on the https://www.flickr.com endpoint. The Request panel displays four query parameters: method set to "flickr.test.echo", format set to "rest", foo set to "bar", a
  3. The request contains all parameters defined in the URL. The Response section is empty because you have not sent the request yet.

    ReadyAPI Response panel showing the Outline tab selected with no response content yet. A placeholder message reads "View an outline of the response's JSON/XML content (Send a request to get a response)". The Smart Assertion button appears in the top right
  4. Click Send to retrieve the response from the server.

    The Response panel displays the parsed XML outline of the Flickr API response. The rsp root node contains the echoed request parameters, confirming the request succeeded (@stat: ok).

    ReadyAPI Response panel showing the Outline tab with the parsed XML response from the Flickr API. The XML Node column displays the rsp root node with child nodes: @stat set to "ok", method set to "flickr.test.echo", format set to "rest", foo set to "bar",

3. Create a Schema

The Schema (conflicts) tab lists any conflicts between the response and the defined schema. Select Auto-Resolve to let ReadyAPI resolve conflicts automatically, or click Resolve conflicts to address them manually.

ReadyAPI Response panel showing the Outline tab with the Schema (conflicts) tab selected and highlighted in orange at the bottom. The Conflicts sub-tab is active, showing an empty conflicts list with an Auto-Resolve checkbox and a Resolve conflicts option

To create a schema manually, ReadyAPI infers it from the response.

  1. Confirm that the Auto-Resolve in the Schema (conflicts) panel is deselected.

  2. Click Resolve Conflicts to resolve conflicts manually.

    ReadyAPI detects each undeclared element in the response schema and prompts you to create it. Click Yes to create the rsp element in the Flickr namespace. Click Yes to all to resolve all similar conflicts at once, or No to skip.

    Note

    No previous schema exists, so you can resolve all conflicts at once. Click Yes to all.

    ReadyAPI Conflict dialog box asking "Undeclared root element. Create element 'rsp' in namespace 'https://www.flickr.com' at path /rsp?" with three options: Yes, Yes to all, and No.

    ReadyAPI resolves all conflicts and adds them to the Schema panel.

    ReadyAPI Response panel showing the Conflicts tab active within the Schema section. The conflicts list highlighted in orange displays all resolved schema conflicts for the Flickr API response, including undeclared elements, illegal content, and undeclared
  3. Select the Schemas tab to view the XML schema generated by ReadyAPI from the resolved conflicts.

    ReadyAPI Response panel showing the Schemas tab active within the Schema section. The Schemas panel highlighted in orange displays the generated XML schema definition for the Flickr API response, with the https://www.flickr.com namespace listed on the lef

    Tip

    You can improve this schema by sending more requests with different parameters. For example, send an invalid request to infer the schema for a fault, or send a request that returns an empty result set.

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.

  1. Open the Conflicts tab and select the Auto-Resolve option.

    ReadyAPI Response panel showing the Conflicts tab active and highlighted in orange within the Schema section. The Auto-Resolve checkbox is checked and highlighted in orange. The conflicts list displays all resolved schema changes for the Flickr API respon
  2. To get a different response, remove the api_key parameter value from the request.

    ReadyAPI Request panel showing four query parameters: method set to "flickr.test.echo", format set to "rest", foo set to "bar", and api_key with an empty value field highlighted in orange. All parameters are of type QUERY. The Generate Values button appea
  3. Click Send to receive the response from the server.

    The Response panel displays the parsed XML outline of the Flickr API response.

    ReadyAPI Response panel showing the Outline tab with the parsed XML response for a failed request. The rsp root node shows @stat set to "fail", with an err child node containing @code set to "100" and @msg set to "Invalid API Key (Key has invalid format)"

    With Auto-Resolve enabled, ReadyAPI automatically resolves new schema conflicts triggered by the invalid request, without prompting you each time.

    ReadyAPI Response panel showing the Conflicts tab active and highlighted in orange within the Schema section. The Auto-Resolve checkbox is checked and highlighted in orange. The conflicts list highlighted in orange displays automatically resolved schema c
  4. Select the Schemas tab to view the updated XML schema. The rsp_err complex type is now included in the schema definition, capturing the error structure returned by an invalid request.

    ReadyAPI Response panel showing the Schemas tab active and highlighted in orange within the Schema section. The left panel lists the https://www.flickr.com namespace. The right panel displays the updated XML schema definition highlighted in orange, showin

    Tip

    Further modify the request to elicit different responses and build a more complete schema.

The following table compares the XML schema before and after sending the invalid request:

Table 45. Resource comparison

Before

After

Initial XML schema generated from the valid request, defining the _rsp complex type with method, format, foo, and api_key elements.

XML schema definition for the Flickr API response showing the initial _rsp complex type with method, format, foo as xs:string elements, api_key as xs:hexBinary, and a stat attribute of xs:string type. No optional elements or error types are defined.

Updated XML schema extended with optional elements (minOccurs="0") and the new rsp_err complex type, capturing the error structure returned by the invalid request.

Updated XML schema definition for the Flickr API response showing the _rsp complex type with method, format, foo, api_key, and err elements all set to minOccurs"0". A new rsp_err complex type is defined with msg as xs:string and code as xs:positiveInteger


5. Use the Schema

Inferred schemas verify that the service response contains no unexpected elements. Use the Schema Compliance assertion to do this:

  1. Expand the Assertions panel and click .

    ReadyAPI Test Step Request 1 editor showing a GET request to the /services/rest/ resource on the https://www.flickr.com endpoint. The Request panel on the left displays method, format, and foo query parameters. The Response panel on the right shows the Ou
  2. In the Add Assertion dialog, click Compliance, Status and Standards , then click Schema Compliance. Click Add Assertion.

    ReadyAPI adds the assertion and automatically verifies the incoming responses.

    Tip

    The schema also appears on the Specification tab of the API editor. In the Navigator, select your API under APIs to open it. If the schema does not appear, click the Recreate WADL icon in the toolbar to refresh the definition.

    ReadyAPI Navigator panel showing REST Project 2 with the https://www.flickr.com API selected and highlighted in orange under APIs. The main editor shows the https://www.flickr.com API with the Specification tab active and highlighted in orange. The left p

See Also

Publication date: