All Products
Document Center

Model Management

Last Updated: May 27, 2020

1. Overview and limits

A model is used to describe data in HTTP requests and responses. You can use JSON Schema to define an API Gateway model and describe the data structure that is defined in APIs. You can define fields that are included in the request and response parameters and the valid values of these fields. After defining a model and referencing this model in an API you created, you can export an SDK for the API. The system will also generate a POJO class for the referenced model. This makes it easier for you to pass in parameters and deserialize response data.

An API Gateway model is defined based on JSON Schema Draft-04. The following limits apply to defining a model:

  1. You can only create JSON Schemas of the object property.
  2. $ref only allows you to reference your own model. You can obtain the reference address of the model from Create Model and Get a model you’ve created. The model does not support circular referencing.

The following code demonstrates how to define a model supported by API Gateway:

  1. {
  2. "required": ["name", "photoUrls"],
  3. "type": "object",
  4. "properties": {
  5. "id": {
  6. "format": "int64",
  7. "type": "integer"
  8. },
  9. "category": {
  10. "$ref": ""
  11. },
  12. "name": {
  13. "pattern": "^\\d{3}-\\d{2}-\\d{4}$",
  14. "type": "string"
  15. },
  16. "status": {
  17. "type": "string"
  18. },
  19. "dogProject": {
  20. "type": "object",
  21. "properties": {
  22. "id": {
  23. "format": "int64",
  24. "maximum": 100,
  25. "exclusiveMaximum": true,
  26. "type": "integer"
  27. },
  28. "name": {
  29. "maxLength": 10,
  30. "type": "string"
  31. }
  32. }
  33. }
  34. }
  35. }

2. Create a model

You can use Create Model that is provided by Alibaba Cloud to create a model. You can also create a model in the API Gateway console.

Create a model in the API Gateway console:

  1. Click API Groups in the left-side navigation pane.
  2. Click View Models in the Actions column to go to the Model Management page. Then,create a model.Model management

Create a model by importing a Swagger file:

API Gateway allows you to create a model by Create APIs By Importing Swagger. After a Swagger file is imported, a model is automatically created for the API group based on the model-related content in the Swagger file. Note: When you create a model by importing a Swagger file, existing models with the same name will be overwritten without confirmation.

3. Modify and view a model

After creating a model, you can view the model on the Model Management page. On the Model Details page, you can view the model name, the model definition, and the URI that is allocated by the system to the model. API Gateway models can reference each other through $ref:{URI}.

If you want to modify an existing model, click Change in the upper-right corner. Note: When a model is modified, its URI does not change.

Model management 2

4. Delete a model

You can delete a model in a group. Note: API Gateway does not maintain the relationship between a model and APIs. When you delete a model, you may fail to export the SDK of the published APIs. Proceed with caution.