subeeshcbabu-zz / swagmock

Mock data generator for swagger api
MIT License
173 stars 38 forks source link

Produces the empty output for definitions with allOf #55

Open ganqqwerty opened 6 years ago

ganqqwerty commented 6 years ago

If you use allOf in your definitions section of the swagger json object, the mock is empty.

This example works fine:

let Swagmock = require('swagmock');
let Mockgen = Swagmock(api);

Mockgen.responses({
        path: '/news/categories/{categoryId}/articles',
// operation: 'get',
        // response: 200,
        useExamples: true
    })
    .then(mock => {
        console.log('success');
        console.log(JSON.stringify(mock, null, 2));
    }).catch(error => {
    console.error(error);
});
{
    "swagger": "2.0",
    "info": {
        "description": "API managing all news content served to the portal",
        "version": "1.0.0",
        "title": "News API"
    },
    "host": "www.hauni.local",
    "basePath": "/o/api/news/v1",
    "tags": [
        {
            "name": "Article",
            "description": "Article Services"
        },
        {
            "name": "Category",
            "description": "Category Services"
        }
    ],
    "schemes": [
        "http"
    ],
    "paths": {
        "/news/categories/{categoryId}/articles": {
            "get": {
                "tags": [
                    "Article"
                ],
                "summary": "Find News by CategoryId",
                "description": "Returns a list of news of the required category",
                "operationId": "getArticlesByCategoryId",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "categoryId",
                        "in": "path",
                        "description": "Category ID required ",
                        "required": true,
                        "type": "integer",
                        "format": "int64"
                    },
                    {
                        "name": "pageSize",
                        "in": "query",
                        "description": "Expected page size",
                        "type": "integer"
                    },
                    {
                        "name": "pageNumber",
                        "in": "query",
                        "description": "Page number we want to be returned",
                        "type": "integer"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/PagedArticles"
                        }
                    },
                    "404": {
                        "description": "CategoryId not found"
                    }
                }
            }
        }
    },
    "definitions": {
        "PagedArticles": {
          "properties": {
            "totalItems": {
              "type": "number",
              "description": "Total items for this query"
            },
            "pageNumber": {
              "type": "number",
              "description": "Current page number"
            },
            "pageSize": {
              "type": "number",
              "description": "Page size used to provide this result"
            }
          },
          "example": {
            "totalItems": 2,
            "pageNumber": 1,
            "pageSize": 10
          }

        }
    }
}

With this json file it produces the following output, which is good and nice:

success
{
  "get": {
    "responses": {
      "200": {
        "totalItems": -151274603832.9344,
        "pageNumber": -120071625323.3152,
        "pageSize": -558157441846.4768
      }
    }
  }
}

However with the following json we get an empty object, which is a bug:

{
    "swagger": "2.0",
    "info": {
        "description": "API managing all news content served to the portal",
        "version": "1.0.0",
        "title": "News API"
    },
    "host": "www.hauni.local",
    "basePath": "/o/api/news/v1",
    "tags": [
        {
            "name": "Article",
            "description": "Article Services"
        },
        {
            "name": "Category",
            "description": "Category Services"
        }
    ],
    "schemes": [
        "http"
    ],
    "paths": {
        "/news/categories/{categoryId}/articles": {
            "get": {
                "tags": [
                    "Article"
                ],
                "summary": "Find News by CategoryId",
                "description": "Returns a list of news of the required category",
                "operationId": "getArticlesByCategoryId",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "categoryId",
                        "in": "path",
                        "description": "Category ID required ",
                        "required": true,
                        "type": "integer",
                        "format": "int64"
                    },
                    {
                        "name": "pageSize",
                        "in": "query",
                        "description": "Expected page size",
                        "type": "integer"
                    },
                    {
                        "name": "pageNumber",
                        "in": "query",
                        "description": "Page number we want to be returned",
                        "type": "integer"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/PagedArticles"
                        }
                    },
                    "404": {
                        "description": "CategoryId not found"
                    }
                }
            }
        }
    },
    "definitions": {
        "PagedArticles": {
            "allOf": [
                {
                    "$ref": "#/definitions/Pagination"
                }

            ]
        },
        "Pagination": {
            "properties": {
                "totalItems": {
                    "type": "number",
                    "description": "Total items for this query"
                },
                "pageNumber": {
                    "type": "number",
                    "description": "Current page number"
                },
                "pageSize": {
                    "type": "number",
                    "description": "Page size used to provide this result"
                }
            },
            "example": {
                "totalItems": 2,
                "pageNumber": 1,
                "pageSize": 10
            }
        }
    }
}

The output:

{
  "get": {
    "responses": {
      "200": {}
    }
  }
}
ganqqwerty commented 6 years ago

I found that in function Generators/objectMock() we have nothing to process oneOf, anyOf, allOf or not keywords from swagger/openAPI specification. I can provide a relatively naïve implementation for this case, but I have to be sure that @subeeshcbabu is merging pull requests, and didn't abandon the repo.