From 770197296de88118bf80de637025a8ea372716f8 Mon Sep 17 00:00:00 2001 From: "jonas-lt@live.dk" Date: Tue, 29 Aug 2023 13:53:52 +0200 Subject: [PATCH] remove extension --- library/loaders/remove-hashbag-loader.js | 3 + library/src/components/Schema.tsx | 67 +++++++++++-------- .../src/components/__tests__/Schema.test.tsx | 5 +- library/src/helpers/__tests__/schema.test.ts | 32 ++++----- library/src/helpers/schema.ts | 30 +++++---- 5 files changed, 75 insertions(+), 62 deletions(-) diff --git a/library/loaders/remove-hashbag-loader.js b/library/loaders/remove-hashbag-loader.js index 8d421e6c9..43960049f 100644 --- a/library/loaders/remove-hashbag-loader.js +++ b/library/loaders/remove-hashbag-loader.js @@ -1,3 +1,6 @@ +/** + * Make sure code does not contain properties such as `#property` + */ module.exports = function(source) { return source.replace(/^#! .*\n/, ''); }; diff --git a/library/src/components/Schema.tsx b/library/src/components/Schema.tsx index b961a33e2..0293bd994 100644 --- a/library/src/components/Schema.tsx +++ b/library/src/components/Schema.tsx @@ -58,22 +58,18 @@ export const Schema: React.FunctionComponent = ({ const constraints = SchemaHelpers.humanizeConstraints(schema); const externalDocs = schema.externalDocs(); - const renderTypeExt = schema.extensions().get(SchemaHelpers.extRenderType); - const renderType = renderTypeExt?.value() !== false; - const rawValueExt = schema.extensions().get(SchemaHelpers.extRawValue); const rawValue = rawValueExt?.value() === true; - const parameterLocationExt = schema.extensions().get(SchemaHelpers.extParameterLocation); + const parameterLocationExt = schema + .extensions() + .get(SchemaHelpers.extParameterLocation); const parameterLocation = parameterLocationExt?.value() === true; - + let schemaType = SchemaHelpers.toSchemaType(schema); const isExpandable = SchemaHelpers.isExpandable(schema) || dependentSchemas; - isCircular = - isCircular || - schema.isCircular() || - false; + isCircular = isCircular || schema.isCircular() || false; const uid = schema.$id(); const styledSchemaName = isProperty ? 'italic' : ''; const renderedSchemaName = @@ -150,11 +146,9 @@ export const Schema: React.FunctionComponent = ({ ) : (
- {renderType && ( -
- {isCircular ? `${schemaType} [CIRCULAR]` : schemaType} -
- )} +
+ {isCircular ? `${schemaType} [CIRCULAR]` : schemaType} +
{schema.format() && ( @@ -279,7 +273,8 @@ export const Schema: React.FunctionComponent = ({ {schema.oneOf() && schema - .oneOf()?.map((s, idx) => ( + .oneOf() + ?.map((s, idx) => ( = ({ ))} {schema.anyOf() && schema - .anyOf()?.map((s, idx) => ( + .anyOf() + ?.map((s, idx) => ( = ({ ))} {schema.allOf() && schema - .allOf()?.map((s, idx) => ( + .allOf() + ?.map((s, idx) => ( = ({ key={propertyName} /> ))} - {Object.entries(patternProperties || {}).map(([propertyName, property]) => ( - - ))} + {Object.entries(patternProperties || {}).map( + ([propertyName, property]) => ( + + ), + )} ); }; @@ -404,7 +403,12 @@ interface AdditionalPropertiesProps { const AdditionalProperties: React.FunctionComponent = ({ schema, }) => { - if (schema.extensions().get(SchemaHelpers.extRenderAdditionalInfo)?.value() === false) { + if ( + schema + .extensions() + .get(SchemaHelpers.extRenderAdditionalInfo) + ?.value() === false + ) { return null; } @@ -470,7 +474,12 @@ interface AdditionalItemsProps { const AdditionalItems: React.FunctionComponent = ({ schema, }) => { - if (schema.extensions().get(SchemaHelpers.extRenderAdditionalInfo)?.value() === false) { + if ( + schema + .extensions() + .get(SchemaHelpers.extRenderAdditionalInfo) + ?.value() === false + ) { return null; } diff --git a/library/src/components/__tests__/Schema.test.tsx b/library/src/components/__tests__/Schema.test.tsx index a89341820..e7abc1c47 100644 --- a/library/src/components/__tests__/Schema.test.tsx +++ b/library/src/components/__tests__/Schema.test.tsx @@ -4,12 +4,12 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; -import {SchemaV2 as SchemaModel } from '@asyncapi/parser'; +import { SchemaV2 as SchemaModel } from '@asyncapi/parser'; import { Schema } from '../Schema'; describe('Schema component', () => { - test('should work with circular references in schema - using `x-parser-circular-props` extensions', async () => { + test('should work with circular references in schema', async () => { const schema = { type: 'object', properties: { @@ -22,7 +22,6 @@ describe('Schema component', () => { }; schema.properties.circular = schema; schema.properties.circularTwo = schema; - schema['x-schema-private-render-type'] = true; const schemaModel = new SchemaModel(schema as any); render(); diff --git a/library/src/helpers/__tests__/schema.test.ts b/library/src/helpers/__tests__/schema.test.ts index 8b2737d9b..9e9b5b4c3 100644 --- a/library/src/helpers/__tests__/schema.test.ts +++ b/library/src/helpers/__tests__/schema.test.ts @@ -1,5 +1,11 @@ import { SchemaHelpers, SchemaCustomTypes } from '../schema'; -import {SchemaV2 as Schema, ServerVariableV2 as ServerVariable, ChannelParameterV2 as ChannelParameter, ServerVariablesInterface, ChannelParametersInterface } from '@asyncapi/parser'; +import { + SchemaV2 as Schema, + ServerVariableV2 as ServerVariable, + ChannelParameterV2 as ChannelParameter, + ServerVariablesInterface, + ChannelParametersInterface, +} from '@asyncapi/parser'; describe('SchemaHelpers', () => { describe('.toSchemaType', () => { @@ -411,7 +417,7 @@ describe('SchemaHelpers', () => { describe('.serverVariablesToSchema', () => { test('should transform variables to schema', () => { - const variables = { + const variables = ({ foo: new ServerVariable({ enum: ['foo', 'bar'], default: 'foo' }), bar: new ServerVariable({ enum: ['foo', 'bar'], @@ -419,7 +425,7 @@ describe('SchemaHelpers', () => { examples: ['foo', 'bar'], description: 'Some description', }), - } as unknown as ServerVariablesInterface; + } as unknown) as ServerVariablesInterface; const schema = new Schema({ type: 'object', properties: { @@ -438,7 +444,6 @@ describe('SchemaHelpers', () => { }, required: ['foo', 'bar'], 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.serverVariablesToSchema(variables); expect(result).toEqual(schema); @@ -447,14 +452,14 @@ describe('SchemaHelpers', () => { describe('.parametersToSchema', () => { test('should transform parameters to schema', () => { - const parameters = { + const parameters = ({ foo: new ChannelParameter({ schema: { type: 'string' } }), bar: new ChannelParameter({ schema: { type: 'string' }, location: '$message.payload#/user/id', description: 'Some description', }), - } as unknown as ChannelParametersInterface; + } as unknown) as ChannelParametersInterface; const schema = new Schema({ type: 'object', properties: { @@ -471,21 +476,20 @@ describe('SchemaHelpers', () => { }, required: ['foo', 'bar'], 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.parametersToSchema(parameters); expect(result).toEqual(schema); }); test('should handle empty schema of parameter', () => { - const parameters = { + const parameters = ({ foo: new ChannelParameter({ description: 'Some description', }), bar: new ChannelParameter({ location: '$message.payload#/user/id', }), - } as unknown as ChannelParametersInterface; + } as unknown) as ChannelParametersInterface; const schema = new Schema({ type: 'object', properties: { @@ -500,7 +504,6 @@ describe('SchemaHelpers', () => { }, required: ['foo', 'bar'], 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.parametersToSchema(parameters); expect(result).toEqual(schema); @@ -563,7 +566,6 @@ describe('SchemaHelpers', () => { }, ], 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.jsonToSchema(json); expect(result).toEqual(schema); @@ -583,7 +585,6 @@ describe('SchemaHelpers', () => { }, }, 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.jsonToSchema(json); expect(result).toEqual(schema); @@ -617,7 +618,6 @@ describe('SchemaHelpers', () => { }, ], 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }, bar: { type: 'string', @@ -626,7 +626,6 @@ describe('SchemaHelpers', () => { }, }, 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.jsonToSchema(json); expect(result).toEqual(schema); @@ -753,8 +752,7 @@ describe('SchemaHelpers', () => { }, }, required: ['name'], - }, - ); + }); const result = SchemaHelpers.getDependentSchemas(schema); expect(result).toEqual(undefined); @@ -793,7 +791,6 @@ describe('SchemaHelpers', () => { }, }, 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.getDependentSchemas(schema); @@ -845,7 +842,6 @@ describe('SchemaHelpers', () => { }, }, 'x-schema-private-render-additional-info': false, - 'x-schema-private-render-type': false, }); const result = SchemaHelpers.getDependentSchemas(schema); diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index a12014874..b1f454cbd 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -1,4 +1,11 @@ -import { SchemaInterface, ChannelParametersInterface, ServerVariablesInterface, SchemaV2 as SchemaClass, ExtensionInterface, ExtensionsInterface } from '@asyncapi/parser'; +import { + SchemaInterface, + ChannelParametersInterface, + ServerVariablesInterface, + SchemaV2 as SchemaClass, + ExtensionInterface, + ExtensionsInterface, +} from '@asyncapi/parser'; export enum SchemaCustomTypes { // for `true` and `{}` schemas @@ -53,7 +60,6 @@ const jsonSchemaKeywordTypes: Record = { const jsonSchemaKeywords = Object.keys(jsonSchemaKeywordTypes); export class SchemaHelpers { - static extRenderType = 'x-schema-private-render-type'; static extRenderAdditionalInfo = 'x-schema-private-render-additional-info'; static extRawValue = 'x-schema-private-raw-value'; static extParameterLocation = 'x-schema-private-parameter-location'; @@ -210,7 +216,6 @@ export class SchemaHelpers { {}, ), required: Object.keys(urlVariables), - [this.extRenderType]: false, [this.extRenderAdditionalInfo]: false, }; return new SchemaClass(json as any); @@ -240,7 +245,6 @@ export class SchemaHelpers { {}, ), required: Object.keys(parameters), - [this.extRenderType]: false, [this.extRenderAdditionalInfo]: false, }; return new SchemaClass(json as any); @@ -262,7 +266,7 @@ export class SchemaHelpers { return; } const extensions = value.extensions() as ExtensionsInterface; - const filteredExtensions = {} + const filteredExtensions = {}; for (const ext of Object.values(extensions.all())) { const extType = ext as ExtensionInterface; if ( @@ -304,7 +308,9 @@ export class SchemaHelpers { * * @param schema */ - static getDependentSchemas(schema: SchemaInterface): SchemaInterface | undefined { + static getDependentSchemas( + schema: SchemaInterface, + ): SchemaInterface | undefined { const dependencies = schema.dependencies(); if (!dependencies) { return; @@ -329,7 +335,6 @@ export class SchemaHelpers { }, {}, ), - [this.extRenderType]: false, [this.extRenderAdditionalInfo]: false, }; return new SchemaClass(json as any); @@ -352,12 +357,15 @@ export class SchemaHelpers { return type; } - private static toItemsType(items: SchemaInterface[], schema: SchemaInterface): string { + private static toItemsType( + items: SchemaInterface[], + schema: SchemaInterface, + ): string { const types = items.map(item => this.toSchemaType(item)).join(', '); const additionalItems = schema.additionalItems(); if (additionalItems !== undefined && additionalItems !== false) { - const additionalType = - additionalItems === true + const additionalType = + additionalItems === true ? SchemaCustomTypes.ANY : this.toSchemaType(additionalItems); return `tuple<${types || @@ -506,7 +514,6 @@ export class SchemaHelpers { return { type: 'array', items: value.map(v => this.jsonFieldToSchema(v)), - [this.extRenderType]: false, [this.extRenderAdditionalInfo]: false, }; } @@ -516,7 +523,6 @@ export class SchemaHelpers { obj[k] = this.jsonFieldToSchema(v); return obj; }, {}), - [this.extRenderType]: false, [this.extRenderAdditionalInfo]: false, }; }