From d5a6fa980a967d51d51877567aac8bdc7b26625c Mon Sep 17 00:00:00 2001 From: RubyLiu Date: Thu, 27 Jul 2023 13:25:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3json-scheme=E5=B4=A9=E6=BA=83=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/commons/json-schema/convert/convert.js | 9 ++++----- .../definition/components/ApiVariableJson.vue | 11 +++++++++-- .../business/definition/components/body/ApiBody.vue | 5 +++-- .../components/mock/Components/MockApiBody.vue | 3 ++- .../mock/Components/MockApiResponseBody.vue | 11 +++++++++-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/api-test/frontend/src/business/commons/json-schema/convert/convert.js b/api-test/frontend/src/business/commons/json-schema/convert/convert.js index 2ea21b2f15..579331fc1d 100644 --- a/api-test/frontend/src/business/commons/json-schema/convert/convert.js +++ b/api-test/frontend/src/business/commons/json-schema/convert/convert.js @@ -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'; diff --git a/api-test/frontend/src/business/definition/components/ApiVariableJson.vue b/api-test/frontend/src/business/definition/components/ApiVariableJson.vue index fcf522ad6e..7e2b931535 100644 --- a/api-test/frontend/src/business/definition/components/ApiVariableJson.vue +++ b/api-test/frontend/src/business/definition/components/ApiVariableJson.vue @@ -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) { diff --git a/api-test/frontend/src/business/definition/components/body/ApiBody.vue b/api-test/frontend/src/business/definition/components/body/ApiBody.vue index 32de6f5511..ea3669134f 100644 --- a/api-test/frontend/src/business/definition/components/body/ApiBody.vue +++ b/api-test/frontend/src/business/definition/components/body/ApiBody.vue @@ -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'; diff --git a/api-test/frontend/src/business/definition/components/mock/Components/MockApiBody.vue b/api-test/frontend/src/business/definition/components/mock/Components/MockApiBody.vue index a859f193ef..939638de74 100644 --- a/api-test/frontend/src/business/definition/components/mock/Components/MockApiBody.vue +++ b/api-test/frontend/src/business/definition/components/mock/Components/MockApiBody.vue @@ -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'; diff --git a/api-test/frontend/src/business/definition/components/mock/Components/MockApiResponseBody.vue b/api-test/frontend/src/business/definition/components/mock/Components/MockApiResponseBody.vue index 836649d1fb..44b66f9479 100644 --- a/api-test/frontend/src/business/definition/components/mock/Components/MockApiResponseBody.vue +++ b/api-test/frontend/src/business/definition/components/mock/Components/MockApiResponseBody.vue @@ -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) {