From c30d69060a6ea7e9ba8641fe3198ecb903560f3f Mon Sep 17 00:00:00 2001 From: SB-harshitajadhav <165134656+SB-harshitajadhav@users.noreply.github.com> Date: Fri, 10 May 2024 18:33:26 +0530 Subject: [PATCH] fix: is not displaying in the docs (#257) * fix: is not displaying in the docs * fix: added updated default-schema * fix: addressed review comment * fix: addressed review comment * fix: added documentation * fix: reverted changes --- src/__fixtures__/default-schema.json | 5 ++++ src/components/__tests__/SchemaRow.spec.tsx | 29 +++++++++++++++++++++ src/utils/getApplicableFormats.ts | 13 +++++++++ 3 files changed, 47 insertions(+) diff --git a/src/__fixtures__/default-schema.json b/src/__fixtures__/default-schema.json index 4167fd7a..8cb3daf4 100644 --- a/src/__fixtures__/default-schema.json +++ b/src/__fixtures__/default-schema.json @@ -2,6 +2,11 @@ "title": "User", "type": "object", "properties": { + "profile_photo": { + "type": "string", + "contentMediaType": "application/octet-stream", + "description": "This is user's profile photo" + }, "name": { "type": "string", "const": "Constant name", diff --git a/src/components/__tests__/SchemaRow.spec.tsx b/src/components/__tests__/SchemaRow.spec.tsx index bd6351e2..65f71425 100644 --- a/src/components/__tests__/SchemaRow.spec.tsx +++ b/src/components/__tests__/SchemaRow.spec.tsx @@ -260,4 +260,33 @@ describe('SchemaRow component', () => { }); }); }); + describe('schema node with contentMediaType', () => { + let schema: JSONSchema4; + + beforeEach(() => { + schema = { + type: 'object', + properties: { + profile_photo: { + type: 'string', + contentMediaType: 'application/octet-stream', + description: "This is user's profile photo", + }, + }, + }; + }); + + it('should render correct type name for binary type', () => { + const tree = buildTree(schema); + + const schemaNode = findNodeWithPath(tree, ['properties', 'profile_photo']); + if (!schemaNode) { + throw Error('Node not found, invalid configuration'); + } + const wrapper = mount(); + const spanWrapper = wrapper.find({ 'data-test': 'property-type' }); + expect(spanWrapper.at(0).text()).toContain('string'); + wrapper.unmount(); + }); + }); }); diff --git a/src/utils/getApplicableFormats.ts b/src/utils/getApplicableFormats.ts index a4d49e92..b689c514 100644 --- a/src/utils/getApplicableFormats.ts +++ b/src/utils/getApplicableFormats.ts @@ -3,6 +3,19 @@ import { RegularNode, SchemaNodeKind } from '@stoplight/json-schema-tree'; import { COMMON_JSON_SCHEMA_AND_OAS_FORMATS } from '../consts'; export function getApplicableFormats(schemaNode: RegularNode): [type: SchemaNodeKind, format: string] | null { + // JSON Schema itself doesn't directly support defining binary data types. + // Within the http-spec repository, we address this limitation using + // OpenAPI features i.e. `contentMediaType: 'application/octet-stream'`. + // which is specific to OpenAPI and not supported by JSON Schema itself. + + if ( + schemaNode.fragment['contentMediaType'] === 'application/octet-stream' && + schemaNode.types && + schemaNode.types.length > 0 + ) { + return [schemaNode.types[0], 'binary']; + } + if (schemaNode.format === null) { return null; }