earldouglas / swagger-test

Specification-driven REST API testing
MIT License
113 stars 19 forks source link

Swagger 2.0 compatibility issue response => responses #10

Closed Trenrod closed 7 years ago

Trenrod commented 7 years ago

I just updated my api to swagger 2.0. After all test failed I noticed that all x-amples testcases using responses are undefined after parseing.

Did I miss something?

earldouglas commented 7 years ago

Any chance you can show part of your new Swagger spec?

Trenrod commented 7 years ago

Sure:

Below is an example of a REST api. As you see i'm using in my x-amples tests "responses" instead of "response"

/**
 * @swagger
 * /api/auth/createAccountWithCustomerAndUCServer:
 *   post:
 *     tags:
 *       - Register
 *     description: Creates a new account with the given credentials, a
 *       customer and a ucserver. This customer will be administred by default
 *       trader.
 *     consumes:
 *       - application/json
 *     produces:
 *       - application/json
 *     parameters:
 *       - $ref : '#/parameters/Credentials'
 *     responses:
 *       200:
 *         description: Created UCServer, Account and Customer Entries
 *         schema:
 *           type: object
 *           properties:
 *             data:
 *               type: object
 *               properties:
 *                 ucserver:
 *                   $ref: '#/definitions/UCServer'
 *                 account:
 *                   $ref: '#/definitions/Account'
 *                 customer:
 *                   $ref: '#/definitions/Customer'
 *       400:
 *         description: Missing or invalid parameters
 *         schema:
 *           $ref: '#/definitions/Error'
 *       500:
 *         description: Internal server error
 *         schema:
 *           $ref: '#/definitions/Error'
 * #############################################################
 * # Request-Response test
 * #############################################################
 *     x-amples:
 *       - description: "Error missing body"
 *         index: asdasd
 *         request:
 *           params:
 *             id: 123
 *         responses:
 *           status: 500
 *           body:
 *             error:
 *               iErrorDetail: 114
 *         apikey:
 *           $testRef: USER_CREDENTIALS_CUSTOMER
 *       - description: "Error missing body password"
 *         request:
 *           body:
 *             username: 'ado@xxx.de'
 *         responses:
 *           status: 500
 *           body:
 *             error:
 *               iErrorDetail: 114
 *       - description: "Error invalid email"
 *         request:
 *           body:
 *             email: 'adoxxx.de'
 *             password: 'asdflkjsadf'
 *         responses:
 *           status: 500
 *           body:
 *             error:
 *               iErrorDetail: 113
 *       - description: "Create user as customer with ucserver"
 *         request:
 *           body:
 *             $testRef: USER_CREDENTIALS_CUSTOMER
 *         responses:
 *           status: 200
 *           body:
 *             data:
 *               account: {}
 *               customer: {}
 *               ucserver: {}
 */
var getSwaggerJson = preq.get({uri: SWAGGERURI}).then(function(res) { 
...  
// Here I added my workaround to create a "response" field 
// If I would not do it then the objects in the replace array would have an empty "reponse" item.
swaggerJson.paths[apipath][method]["x-amples"][testIdx].response = swaggerJson.paths[apipath][method]["x-amples"][testIdx].responses;
....
  var replaced = recursiveSubStringReplace(swaggerTest.parse(res.body));
}....

EDIT: If i use "response" instead of "responses" following error appears: You are using properties to be deprecated in v2.0.0 Please update to align with the swagger v2.0 spec. [ 'response' ]

"swagger-jsdoc": "1.8.3", "swagger-test": "0.5.1"

earldouglas commented 7 years ago

Interesting, this seems like a misleading error from Swagger's parser. x-amples is considered an extension field, and so it should be permitted to have fields it likes without impacting the rest, or the spec or the way it's parsed.

earldouglas commented 7 years ago

Does #12 look like it might work? In particular, see how the spec changes: https://github.com/earldouglas/swagger-test/pull/12/files#diff-274d41a0f4a07091469ebf98fc0d457a

Trenrod commented 7 years ago

Just tested #12 locally. It works.

earldouglas commented 7 years ago

Excellent; I'll merge it and release as version 0.6.0.

earldouglas commented 7 years ago

This is now available from npmjs.com as version 0.6.0.