Skip to content

Commit

Permalink
fix schema rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Aug 25, 2023
1 parent 1efa3a7 commit 8cf921d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 62 deletions.
40 changes: 5 additions & 35 deletions library/src/components/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export const Schema: React.FunctionComponent<Props> = ({
const externalDocs = schema.externalDocs();

const renderTypeExt = schema.extensions().get(SchemaHelpers.extRenderType);
const renderType = renderTypeExt !== undefined && renderTypeExt.value() !== false;
const renderType = renderTypeExt?.value() !== false;

const rawValueExt = schema.extensions().get(SchemaHelpers.extRawValue);
const rawValue = rawValueExt !== undefined && rawValueExt.value() === true;
const rawValue = rawValueExt?.value() === true;

const parameterLocationExt = schema.extensions().get(SchemaHelpers.extParameterLocation);
const parameterLocation = parameterLocationExt !== undefined && parameterLocationExt.value() === true;
const parameterLocation = parameterLocationExt?.value() === true;

let schemaType = SchemaHelpers.toSchemaType(schema);
const isExpandable = SchemaHelpers.isExpandable(schema) || dependentSchemas;
Expand All @@ -75,35 +75,6 @@ export const Schema: React.FunctionComponent<Props> = ({
schema.isCircular() ||
false;
const uid = schema.$id();

const schemaItems = schema.items();
if (schemaItems && !Array.isArray(schemaItems)) {
/**
* fallback for older logic for circular references:
*
* checking uid for circular items
* `x-parser-circular` extension is added to every schema which has circular `items` field,
* so we must check that `items` is schema (not array of schemas) and infer UID of schema to display which schema is circular (by the name of schema)
*/
isCircular =
isCircular ||
schemaItems.isCircular() ||
false;
if (
isCircular &&
typeof (schemaItems as any).circularSchema === 'function'
) {
schemaType = SchemaHelpers.toSchemaType(
schemaItems
);
}
} else if (
isCircular &&
typeof (schema as any).circularSchema === 'function'
) {
schemaType = SchemaHelpers.toSchemaType((schema as any).circularSchema());
}

const styledSchemaName = isProperty ? 'italic' : '';
const renderedSchemaName =
typeof schemaName === 'string' ? (
Expand Down Expand Up @@ -395,7 +366,6 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({

const required = schema.required() || [];
const patternProperties = schema.patternProperties();
const circularProps = schema.extensions().get('x-parser-circular-props')?.value() || [];

return (
<>
Expand All @@ -405,7 +375,7 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
schemaName={propertyName}
required={required.includes(propertyName)}
isProperty={true}
isCircular={circularProps.includes(propertyName)}
isCircular={property.isCircular()}
dependentRequired={SchemaHelpers.getDependentRequired(
propertyName,
schema,
Expand All @@ -419,7 +389,7 @@ const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
schemaName={propertyName}
isPatternProperty={true}
isProperty={true}
isCircular={circularProps.includes(propertyName)}
isCircular={property.isCircular()}
key={propertyName}
/>
))}
Expand Down
28 changes: 1 addition & 27 deletions library/src/components/__tests__/Schema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,7 @@ describe('Schema component', () => {
};
schema.properties.circular = schema;
schema.properties.circularTwo = schema;
const schemaModel = new SchemaModel(schema as any);

render(<Schema schema={schemaModel} />);

// properties
expect(screen.getByText('nonCircular')).toBeDefined();
expect(screen.getByText('circular')).toBeDefined();
expect(screen.getByText('circularTwo')).toBeDefined();
// [CIRCULAR] annotation
expect(screen.getAllByText('object [CIRCULAR]')).toBeDefined();
expect(screen.getAllByText('object [CIRCULAR]')).toHaveLength(2);
});

test('should work with circular references in schema - using `x-parser-circular-props` extensions', async () => {
const schema = {
type: 'object',
properties: {
nonCircular: {
type: 'string',
},
circular: {},
circularTwo: {},
},
};
schema.properties.circular = schema;
schema.properties.circularTwo = schema;
schema['x-parser-circular-props'] = ['circular', 'circularTwo'];
schema['x-schema-private-render-type'] = true;
const schemaModel = new SchemaModel(schema as any);

render(<Schema schema={schemaModel} />);
Expand Down

0 comments on commit 8cf921d

Please sign in to comment.