swagger-api/swagger-node

use mock.js to solve static mock data

Opened this issue · 3 comments

use mock.js

in swagger-router.js getMockValue function

these was my changed files

var Mock = require('../lib/mock');


var getMockValue = function (version, schema) {
  var type = _.isPlainObject(schema) ? schema.type : schema;
  var xmock =_.isPlainObject(schema) ? schema['x-mock'] : schema;
  var Random = Mock.Random;

  var value;
  var temp;


  if(xmock){
    var data = xmock.split("|");
    type =data[0];
    if(data.length>1)
        temp = data[1];
  }
  if (!type) {
    type = 'object';

  }

  switch (type) {

  //x-type
  case 'url':
    value = Random.url();
    break;
  case 'date':
    value = Random.date(temp);
    break;

  case 'array':
      // 默认三个数组
    value = [getMockValue(version, _.isArray(schema.items) ? schema.items[0] : schema.items),
      getMockValue(version, _.isArray(schema.items) ? schema.items[0] : schema.items),
      getMockValue(version, _.isArray(schema.items) ? schema.items[0] : schema.items)
    ];



    break;

  case 'boolean':
    if (version === '1.2' && !_.isUndefined(schema.defaultValue)) {
      value = schema.defaultValue;
    } else if (version === '2.0' && !_.isUndefined(schema.default)) {
      value = schema.default;
    } else if (_.isArray(schema.enum)) {
      value = schema.enum[0];
    } else {
      value = 'true';
    }

    // Convert value if necessary
    value = value === 'true' || value === true ? true : false;

    break;

  case 'file':
  case 'File':
    value = 'Pretend this is some file content';

    break;

  case 'integer':
    if (version === '1.2' && !_.isUndefined(schema.defaultValue)) {
      value = schema.defaultValue;
    } else if (version === '2.0' && !_.isUndefined(schema.default)) {
      value = schema.default;
    } else if (_.isArray(schema.enum)) {
      value = schema.enum[0];
    } else {
      var Random = Mock.Random
      value = Random.integer(1,1000)||1;
    }

    // Convert value if necessary
    if (!_.isNumber(value)) {
      value = parseInt(value, 10);
    }

    // TODO: Handle constraints and formats

    break;

  case 'object':
    value = {};

    _.each(schema.allOf, function (parentSchema) {
      _.each(parentSchema.properties, function (property, propName) {
        value[propName] = getMockValue(version, property);
      });
    });

    _.each(schema.properties, function (property, propName) {
      value[propName] = getMockValue(version, property);
    });

    break;

  case 'number':
    if (version === '1.2' && !_.isUndefined(schema.defaultValue)) {
      value = schema.defaultValue;
    } else if (version === '2.0' && !_.isUndefined(schema.default)) {
      value = schema.default;
    } else if (_.isArray(schema.enum)) {
      value = schema.enum[0];
    } else {
      // value = 1.0;

      value = Random.url();

    }

    // Convert value if necessary
    if (!_.isNumber(value)) {
      value = parseFloat(value);
    }

    // TODO: Handle constraints and formats

    break;

  case 'string':
    if (version === '1.2' && !_.isUndefined(schema.defaultValue)) {
      value = schema.defaultValue;
    } else if (version === '2.0' && !_.isUndefined(schema.default)) {
      value = schema.default;
    } else if (_.isArray(schema.enum)) {
      value = schema.enum[0];
    } else {
      if (schema.format === 'date') {
        value = new Date().toISOString().split('T')[0];
      } else if (schema.format === 'date-time') {
        value = new Date().toISOString();

      } else {
        // var Random = Mock.Random
        value = Random.name();
        // value = 'Sample text';
      }
    }

    break;

  }

  return value;
};

you can custom mock type like 'x-mock: date' in swagger.yaml

definitions:
  Product:
    type: object
    properties:
      product_id:
        type: string
        description: Unique identifier representing a specific product for a given latitude & longitude. For example, uberX in San Francisco will have a different product_id than uberX in Los Angeles.
      description:
        type: string
        description: Description of product.
      display_name:
        type: string
        description: Display name of product.
      capacity:
        type: string
       x-mock: date
        description: Capacity of product. For example, 4 people.
      image:
        type: string
        description: Image URL representing the product.

@longzhiyou 你是否提供这方面更多的资料,例如博客文章之类的~~

最近在尝试搭建类似的系统给团队用,一直在用swagger。找了不少类似的解决方案,都不是很完美,达不到开箱即用。本以为swagger-node会满足,但其mock的数据太弱了。

有幸看到你的这个issue,非常符合我的需求,只是感觉描述的太粗了期待你的回复

@kazaff ,我看了你写的如何基于接口文档生成模拟数据

想了解一下,我司用的是java生成的swagger,然后如果准备拆多个文件,具体怎么搞呢?

Measy commented

@kazaff
感觉你直接在路由里面去造模拟数据还是太生硬,有些数据如果有需要保存下来在特定的请求下面做到能控制返回哪一个特定的报文这块控制不是很强