Skip to content

Commit

Permalink
fix(接口测试): 修复前端精度问题
Browse files Browse the repository at this point in the history
--bug=1026169 --user=王孝刚
[接口测试]github#24378接口测试json请求体用json-schema转换后再关闭json-schema,数据内容发生了变化
https://www.tapd.cn/55049933/s/1371684
  • Loading branch information
wxg0103 authored and fit2-zhao committed May 16, 2023
1 parent 7682470 commit b0ad6e3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import MsApiBinaryVariable from "./ApiBinaryVariable";
import MsApiFromUrlVariable from "./ApiFromUrlVariable";
import BatchAddParameter from "../basis/BatchAddParameter";
import Convert from "@/business/components/common/json-schema/convert/convert";
import {jsonParse, trimAll} from "@/business/components/common/json-schema/convert/jsonParse";
export default {
Expand Down Expand Up @@ -226,12 +227,9 @@ export default {
if (this.body.format === 'JSON-SCHEMA') {
if (this.body.raw) {
try {
if (!this.body.jsonSchema) {
this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw));
} else {
let data = MsConvert.format(JSON.parse(this.body.raw));
this.body.jsonSchema = this.deepAssign(this.body.jsonSchema, data);
}
const tmpStr = trimAll(this.body.raw)
const tmpObj = jsonParse(tmpStr)
this.body.jsonSchema = MsConvert.format(tmpObj);
} catch (e) {
this.body.format = 'JSON';
this.$error(this.$t('api_test.definition.json_format_error'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const isObject = require("lodash.isobject");
const isString = require("lodash.isstring");
const {post} = require("@/common/js/ajax");
const isArray = Array.isArray;

const { default: CustomNum } = require('./customNum');

class Convert {
constructor() {
Expand Down Expand Up @@ -113,7 +113,7 @@ class Convert {
if (!result["properties"]) {
continue;
}
if (isObject(element)) {
if (isObject(element) && !(element instanceof CustomNum)) {
// 创建当前属性的基本信息
result["properties"][key] = this._value2object(element, $id, key)
if (isArray(element)) {
Expand Down Expand Up @@ -234,6 +234,10 @@ class Convert {
} else if (isArray(value)) {
objectTemplate.type = "array";
objectTemplate["mock"] = undefined;
} else if (value instanceof CustomNum) {
// 解决丢失精度问题
objectTemplate.type = 'number';
objectTemplate['mock'].mock = value.get();
} else if (isObject(value)) {
objectTemplate.type = "object"
objectTemplate["mock"] = undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CustomNum{
constructor(value) {
this.value = value
}
get() {
return this.value
}
}
export default CustomNum;
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import CustomNum from './customNum';

export const jsonParse = (jsonStr) => {
let index = 0;
function parseValue() {
let char = jsonStr[index];
if (char === '{') {
return parseObject();
} else if (char === '[') {
return parseArray();
} else if (char === '"') {
return parseString();
} else if (char === 't' || char === 'f') {
return parseBoolean();
} else if (char === 'n') {
return parseNull();
} else {
return parseNumber();
}
}
function parseObject() {
let obj = {};
index++;
while (jsonStr[index] !== '}') {
let key = parseString();
index++;
let value = parseValue();
obj[key] = value;
if (jsonStr[index] === ',') {
index++;
}
}
index++;
return obj;
}
function parseArray() {
let arr = [];
index++;
while (jsonStr[index] !== ']') {
arr.push(parseValue());
if (jsonStr[index] === ',') {
index++;
}
}
index++;
return arr;
}
function parseString() {
let str = '';
index++;
while (jsonStr[index] !== '"') {
str += jsonStr[index];
index++;
}
index++;
return str;
}
function parseNumber() {
let numStr = '';
while (/[0-9.+-]/.test(jsonStr[index])) {
numStr += jsonStr[index];
index++;
}
if (!isInteger(numStr) || numStr.length > 15) {
return new CustomNum(numStr);
}
return parseFloat(numStr);
}
function parseBoolean() {
if (jsonStr[index] === 't') {
index += 4;
return true;
} else {
index += 5;
return false;
}
}
function parseNull() {
index += 4;
return null;
}
return parseValue();
};
export const isInteger = (num) => {
return /^\d+$/.test(num);
};
export const trimAll = (ele) => {
if (typeof ele === 'string') {
return ele.split(/[\t\r\f\n\s]*/g).join('');
}
};

0 comments on commit b0ad6e3

Please sign in to comment.