Skip to content

Commit c30d690

Browse files
fix: <binary> is not displaying in the docs (#257)
* fix: <binary> 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
1 parent eccbba6 commit c30d690

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/__fixtures__/default-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
"title": "User",
33
"type": "object",
44
"properties": {
5+
"profile_photo": {
6+
"type": "string",
7+
"contentMediaType": "application/octet-stream",
8+
"description": "This is user's profile photo"
9+
},
510
"name": {
611
"type": "string",
712
"const": "Constant name",

src/components/__tests__/SchemaRow.spec.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,33 @@ describe('SchemaRow component', () => {
260260
});
261261
});
262262
});
263+
describe('schema node with contentMediaType', () => {
264+
let schema: JSONSchema4;
265+
266+
beforeEach(() => {
267+
schema = {
268+
type: 'object',
269+
properties: {
270+
profile_photo: {
271+
type: 'string',
272+
contentMediaType: 'application/octet-stream',
273+
description: "This is user's profile photo",
274+
},
275+
},
276+
};
277+
});
278+
279+
it('should render correct type name for binary type', () => {
280+
const tree = buildTree(schema);
281+
282+
const schemaNode = findNodeWithPath(tree, ['properties', 'profile_photo']);
283+
if (!schemaNode) {
284+
throw Error('Node not found, invalid configuration');
285+
}
286+
const wrapper = mount(<SchemaRow schemaNode={schemaNode} nestingLevel={0} />);
287+
const spanWrapper = wrapper.find({ 'data-test': 'property-type' });
288+
expect(spanWrapper.at(0).text()).toContain('string<binary>');
289+
wrapper.unmount();
290+
});
291+
});
263292
});

src/utils/getApplicableFormats.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import { RegularNode, SchemaNodeKind } from '@stoplight/json-schema-tree';
33
import { COMMON_JSON_SCHEMA_AND_OAS_FORMATS } from '../consts';
44

55
export function getApplicableFormats(schemaNode: RegularNode): [type: SchemaNodeKind, format: string] | null {
6+
// JSON Schema itself doesn't directly support defining binary data types.
7+
// Within the http-spec repository, we address this limitation using
8+
// OpenAPI features i.e. `contentMediaType: 'application/octet-stream'`.
9+
// which is specific to OpenAPI and not supported by JSON Schema itself.
10+
11+
if (
12+
schemaNode.fragment['contentMediaType'] === 'application/octet-stream' &&
13+
schemaNode.types &&
14+
schemaNode.types.length > 0
15+
) {
16+
return [schemaNode.types[0], 'binary'];
17+
}
18+
619
if (schemaNode.format === null) {
720
return null;
821
}

0 commit comments

Comments
 (0)