Skip to content

Commit

Permalink
remove extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Aug 29, 2023
1 parent d1f9cee commit 7701972
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 62 deletions.
3 changes: 3 additions & 0 deletions library/loaders/remove-hashbag-loader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Make sure code does not contain properties such as `#property`
*/
module.exports = function(source) {
return source.replace(/^#! .*\n/, '');
};
67 changes: 38 additions & 29 deletions library/src/components/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,18 @@ export const Schema: React.FunctionComponent<Props> = ({
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 =
Expand Down Expand Up @@ -150,11 +146,9 @@ export const Schema: React.FunctionComponent<Props> = ({
) : (
<div>
<div>
{renderType && (
<div className="capitalize text-sm text-teal-500 font-bold inline-block mr-2">
{isCircular ? `${schemaType} [CIRCULAR]` : schemaType}
</div>
)}
<div className="capitalize text-sm text-teal-500 font-bold inline-block mr-2">
{isCircular ? `${schemaType} [CIRCULAR]` : schemaType}
</div>
<div className="inline-block">
{schema.format() && (
<span className="bg-yellow-600 font-bold no-underline text-white rounded lowercase mr-2 p-1 text-xs">
Expand Down Expand Up @@ -279,7 +273,8 @@ export const Schema: React.FunctionComponent<Props> = ({

{schema.oneOf() &&
schema
.oneOf()?.map((s, idx) => (
.oneOf()
?.map((s, idx) => (
<Schema
key={idx}
schema={s}
Expand All @@ -288,7 +283,8 @@ export const Schema: React.FunctionComponent<Props> = ({
))}
{schema.anyOf() &&
schema
.anyOf()?.map((s, idx) => (
.anyOf()
?.map((s, idx) => (
<Schema
key={idx}
schema={s}
Expand All @@ -297,7 +293,8 @@ export const Schema: React.FunctionComponent<Props> = ({
))}
{schema.allOf() &&
schema
.allOf()?.map((s, idx) => (
.allOf()
?.map((s, idx) => (
<Schema
key={idx}
schema={s}
Expand Down Expand Up @@ -383,16 +380,18 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
key={propertyName}
/>
))}
{Object.entries(patternProperties || {}).map(([propertyName, property]) => (
<Schema
schema={property}
schemaName={propertyName}
isPatternProperty={true}
isProperty={true}
isCircular={property.isCircular()}
key={propertyName}
/>
))}
{Object.entries(patternProperties || {}).map(
([propertyName, property]) => (
<Schema
schema={property}
schemaName={propertyName}
isPatternProperty={true}
isProperty={true}
isCircular={property.isCircular()}
key={propertyName}
/>
),
)}
</>
);
};
Expand All @@ -404,7 +403,12 @@ interface AdditionalPropertiesProps {
const AdditionalProperties: React.FunctionComponent<AdditionalPropertiesProps> = ({
schema,
}) => {
if (schema.extensions().get(SchemaHelpers.extRenderAdditionalInfo)?.value() === false) {
if (
schema
.extensions()
.get(SchemaHelpers.extRenderAdditionalInfo)
?.value() === false
) {
return null;
}

Expand Down Expand Up @@ -470,7 +474,12 @@ interface AdditionalItemsProps {
const AdditionalItems: React.FunctionComponent<AdditionalItemsProps> = ({
schema,
}) => {
if (schema.extensions().get(SchemaHelpers.extRenderAdditionalInfo)?.value() === false) {
if (
schema
.extensions()
.get(SchemaHelpers.extRenderAdditionalInfo)
?.value() === false
) {
return null;
}

Expand Down
5 changes: 2 additions & 3 deletions library/src/components/__tests__/Schema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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(<Schema schema={schemaModel} />);
Expand Down
32 changes: 14 additions & 18 deletions library/src/helpers/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -411,15 +417,15 @@ 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'],
default: 'bar',
examples: ['foo', 'bar'],
description: 'Some description',
}),
} as unknown as ServerVariablesInterface;
} as unknown) as ServerVariablesInterface;
const schema = new Schema({
type: 'object',
properties: {
Expand All @@ -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);
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -617,7 +618,6 @@ describe('SchemaHelpers', () => {
},
],
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
},
bar: {
type: 'string',
Expand All @@ -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);
Expand Down Expand Up @@ -753,8 +752,7 @@ describe('SchemaHelpers', () => {
},
},
required: ['name'],
},
);
});

const result = SchemaHelpers.getDependentSchemas(schema);
expect(result).toEqual(undefined);
Expand Down Expand Up @@ -793,7 +791,6 @@ describe('SchemaHelpers', () => {
},
},
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
});

const result = SchemaHelpers.getDependentSchemas(schema);
Expand Down Expand Up @@ -845,7 +842,6 @@ describe('SchemaHelpers', () => {
},
},
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
});

const result = SchemaHelpers.getDependentSchemas(schema);
Expand Down
Loading

0 comments on commit 7701972

Please sign in to comment.