From fec880b9bd48f9110a8980e1ea4a31d37440fc76 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Mon, 23 Oct 2023 18:36:30 +0300 Subject: [PATCH] fix: default value as object in request body (#2437) --- src/components/Fields/FieldDetails.tsx | 8 +-- .../__tests__/FieldDetails.test.tsx | 32 +++++++++ .../__snapshots__/FieldDetails.test.tsx.snap | 68 ++++++++++++++++++- 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/src/components/Fields/FieldDetails.tsx b/src/components/Fields/FieldDetails.tsx index 31864f48..996656fa 100644 --- a/src/components/Fields/FieldDetails.tsx +++ b/src/components/Fields/FieldDetails.tsx @@ -51,10 +51,10 @@ export const FieldDetailsComponent = observer((props: FieldProps) => { return null; }, [field, showExamples]); - - const defaultValue = isObject(schema.default) - ? getSerializedValue(field, schema.default).replace(`${field.name}=`, '') - : schema.default; + const defaultValue = + isObject(schema.default) && field.in + ? getSerializedValue(field, schema.default).replace(`${field.name}=`, '') + : schema.default; return (
diff --git a/src/components/__tests__/FieldDetails.test.tsx b/src/components/__tests__/FieldDetails.test.tsx index 703dc3ea..04982bb9 100644 --- a/src/components/__tests__/FieldDetails.test.tsx +++ b/src/components/__tests__/FieldDetails.test.tsx @@ -44,4 +44,36 @@ describe('FieldDetailsComponent', () => { expect(wrapper.render()).toMatchSnapshot(); }); + + it('renders correctly when default value is object in request body', () => { + const mockFieldProps = { + showExamples: true, + field: { + schema: { + type: 'object', + default: { properties: {} }, + displayType: 'object', + title: 'test title', + externalDocs: undefined, + constraints: [''], + } as SchemaModel, + example: 'example', + name: 'name', + expanded: false, + required: false, + kind: '', + deprecated: false, + collapse: jest.fn(), + toggle: jest.fn(), + explode: false, + expand: jest.fn(), + description: 'test description', + in: undefined, + }, + renderDiscriminatorSwitch: jest.fn(), + }; + const wrapper = shallow(withTheme()); + + expect(wrapper.render()).toMatchSnapshot(); + }); }); diff --git a/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap b/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap index 1a700e55..cd9c98c3 100644 --- a/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/FieldDetails.test.tsx.snap @@ -41,7 +41,73 @@ exports[`FieldDetailsComponent renders correctly 1`] = ` - "" + [] + +
+ +
+ + Example: + + + + "example" + +
+
+
+

+ test description +

+ + +
+
+ +`; + +exports[`FieldDetailsComponent renders correctly when default value is object in request body 1`] = ` +
+
+ + + object + + + (test title) + + + + + + + +
+
+ + Default: + + + + {"properties":{}}