fix(接口测试): 解决json-scheme崩溃问题

This commit is contained in:
RubyLiu 2023-07-27 13:25:39 +08:00 committed by rubylliu
parent aef1c260d0
commit d5a6fa980a
5 changed files with 27 additions and 12 deletions

View File

@ -5,9 +5,8 @@ const isNull = require('lodash.isnull');
const isNumber = require('lodash.isnumber');
const isObject = require('lodash.isobject');
const isString = require('lodash.isstring');
const { post } = require('@/api/base-network');
const { schemaToJson, apiPreview } = require('@/api/definition');
const { default: CustomNum } = require('./customNum');
const { isLosslessNumber } = require('lossless-json');
const isArray = Array.isArray;
class Convert {
@ -114,7 +113,7 @@ class Convert {
if (!result['properties']) {
continue;
}
if (isObject(element) && !(element instanceof CustomNum)) {
if (isObject(element) && !(isLosslessNumber(element))) {
// 创建当前属性的基本信息
result['properties'][key] = this._value2object(element, $id, key);
if (isArray(element)) {
@ -236,10 +235,10 @@ class Convert {
} else if (isArray(value)) {
objectTemplate.type = 'array';
objectTemplate['mock'] = undefined;
} else if (value instanceof CustomNum) {
} else if (isLosslessNumber(value)) {
// 解决丢失精度问题
objectTemplate.type = 'number';
objectTemplate['mock'].mock = value.get();
objectTemplate['mock'].mock = value.value.toString();
}
else if (isObject(value)) {
objectTemplate.type = 'object';

View File

@ -20,6 +20,7 @@
import MsCodeEdit from 'metersphere-frontend/src/components/MsCodeEdit';
import Convert from '@/business/commons/json-schema/convert/convert';
import MsJsonCodeEdit from '@/business/commons/json-schema/JsonSchemaEditor';
import { parse } from 'lossless-json';
export default {
name: 'MsApiVariableJson',
@ -75,8 +76,14 @@ export default {
formatChange() {
const MsConvert = new Convert();
if (this.item.jsonType === 'JSON-SCHEMA') {
if (this.item.value && !this.item.jsonSchema) {
this.item.jsonSchema = MsConvert.format(JSON.parse(this.item.value));
if (this.item.value) {
try {
const jsonObj = parse(this.item.value)
this.item.jsonSchema = MsConvert.format(jsonObj);
} catch (e) {
this.body.format = 'JSON';
this.$message.error(this.$t('api_definition.body.json_format_error'));
}
}
} else {
if (this.item.jsonSchema) {

View File

@ -106,6 +106,8 @@ import BatchAddParameter from '../basis/BatchAddParameter';
import Convert from '@/business/commons/json-schema/convert/convert';
import { getApiParamsConfigFields } from 'metersphere-frontend/src/utils/custom_field';
import ApiParamsConfig from '@/business/definition/components/request/components/ApiParamsConfig';
import { parse } from 'lossless-json'
import CustomNum from '../../../commons/json-schema/convert/customNum';
export default {
name: 'MsApiBody',
@ -281,8 +283,7 @@ export default {
if (this.body.format === 'JSON-SCHEMA') {
if (this.body.raw) {
try {
let JSONBig = require('json-bigint')({ storeAsString: true });
const jsonObj = JSONBig.parse(this.body.raw)
const jsonObj = parse(this.body.raw)
this.body.jsonSchema = MsConvert.format(jsonObj);
} catch (e) {
this.body.format = 'JSON';

View File

@ -71,6 +71,7 @@ import MsApiFromUrlVariable from '@/business/definition/components/body/ApiFromU
import BatchAddParameter from '@/business/definition/components/basis/BatchAddParameter';
import Convert from '@/business/commons/json-schema/convert/convert';
import MockCombinationCondition from '@/business/definition/components/mock/Components/MockCombinationCondition';
import { parse } from 'lossless-json';
export default {
name: 'MockApiBody',
@ -198,7 +199,7 @@ export default {
if (this.body.format === 'JSON-SCHEMA') {
if (this.body.raw) {
try {
const jsonObj = JSON.parse(this.body.raw)
const jsonObj = parse(this.body.raw)
this.body.jsonSchema = MsConvert.format(jsonObj);
} catch (e) {
this.body.format = 'JSON';

View File

@ -82,6 +82,7 @@ import MsApiVariable from '@/business/definition/components/ApiVariable';
import MsApiFromUrlVariable from '@/business/definition/components/body/ApiFromUrlVariable';
import BatchAddParameter from '@/business/definition/components/basis/BatchAddParameter';
import Convert from '@/business/commons/json-schema/convert/convert';
import { parse } from 'lossless-json';
export default {
name: 'MockApiResponseBody',
@ -226,8 +227,14 @@ export default {
const MsConvert = new Convert();
if (this.body.format === 'JSON-SCHEMA') {
if (this.body.raw && !this.body.jsonSchema) {
this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw));
if (this.body.raw) {
try {
const jsonObj = parse(this.body.raw)
this.body.jsonSchema = MsConvert.format(jsonObj);
} catch (e) {
this.body.format = 'JSON';
this.$message.error(this.$t('api_definition.body.json_format_error'));
}
}
} else {
if (this.body.jsonSchema) {