Skip to content

Commit

Permalink
fix: <binary> is not displaying in the docs (#257)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SB-harshitajadhav committed May 10, 2024
1 parent eccbba6 commit c30d690
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/__fixtures__/default-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions src/components/__tests__/SchemaRow.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<SchemaRow schemaNode={schemaNode} nestingLevel={0} />);
const spanWrapper = wrapper.find({ 'data-test': 'property-type' });
expect(spanWrapper.at(0).text()).toContain('string<binary>');
wrapper.unmount();
});
});
});
13 changes: 13 additions & 0 deletions src/utils/getApplicableFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c30d690

Please sign in to comment.