Adding examples for Request and Response in openapi.yaml
https://lf-onap.atlassian.net/browse/CPS-774
Issues and Decisions
Issue |
| Solution | ||
|---|---|---|---|---|
| 1 | 1 | API Generation method in DMI-Plugin |
| |
| 2 | 2 | Backward compatibility move from <String> to <object> REST Interfaces for json objects | Need to verify backward compatibility for client URLs | These changes will introduce no changes at the client side. All users can continue to use the API as as they are using currently. This could be verified by executing the CSIT test of cps and dmi-plugin. Test by @Aditya Puthuparambil |
What Is OpenAPI?
OpenAPI Specification (formerly Swagger Specification) is an API description format for REST APIs. An OpenAPI file allows you to describe your entire API, including:
Available endpoints (
/users) and operations on each endpoint (GET /users,POST /users)Operation parameters Input and output for each operation
Authentication methods
Contact information, license, terms of use and other information.
Adding Examples
You can add examples to parameters, properties and objects to make OpenAPI specification of your web service clearer. Examples can be read by tools and libraries that process your API in some way. To specify an example, you use the example or examples keys.
Request and Response Body Examples
Here is an example in a request body:
requestBody:required: truecontent:application/json:schema:type: stringexamples:dataSample:value:test:bookstore:bookstore-name: Chapterscategories: - code: 01name: SciFi - code: 02name: kidsAll request and response body examples in CPS will be added at the object level.
Parameter Examples
Here is an example of a parameter value:
dataspaceNameInPath:name: dataspace-namein: pathdescription: dataspace-namerequired: trueschema:type: stringexample: DataspaceName1
Reusing Examples:
You can define common examples in the components/examples section of your specification and then re-use them in various parameter descriptions, request and response body descriptions, objects and properties:
Sample example defined in components.yaml
components:examples:dataSample:value:test:bookstore:bookstore-name: Chapterscategories: - code: 01name: SciFi - code: 02name: kidssummary: A sample data
Sample response using examples defined in components:
responses:'200':description: OKcontent:application/json:schema:type: objectexample: $ref: 'components.yml#/components/examples/dataSample'
Sample request using examples defined in components:
requestBody:required: truecontent:application/json:schema:type: stringexamples: dataSample:$ref: 'components.yml#/components/examples/dataSample'
Examples for composite components:
DataAccessRequest:type: objectproperties:operation:type: stringenum: [ read, create, update, delete ]example: createdataType:type: stringexample: dataTypeValuedata:type: stringexample: datacmHandleProperties:$ref: '#/components/schemas/cmHandleProperties'cmHandleProperties:type: objectadditionalProperties:type: stringexample: - prop1: valueprop2: value
Schema Type for Request and Response
At present most of the requests are defined as schema type String for Request body json. These could be updated to object type. This will introduce below changes:
S. No | Summary | Current code | Expected code |
|---|---|---|---|
1 | Changing the schema type in openapi. |
|
|
2 | Changes in Controller Changing the datatype to Object |
|
|
| Converting jsonData to String while passing the data to service layer |
|
|
Note: These changes will introduce no changes at the client side. All users can continue to use the API as as they are using currently. This could be verified by executing the CSIT test of cps and dmi-plugin.