Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: oneOf included when inside of arrays #927

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions library/src/components/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Props {
dependentRequired?: string[];
expanded?: boolean;
onlyTitle?: boolean;
isArray?: boolean;
}

const SchemaContext = React.createContext({
Expand All @@ -31,17 +32,22 @@ export const Schema: React.FunctionComponent<Props> = ({
dependentRequired,
expanded: propExpanded = false,
onlyTitle = false,
isArray = false,
}) => {
const { reverse, deepExpanded } = useContext(SchemaContext);
const [expanded, setExpanded] = useState(propExpanded);
const [expanded, setExpanded] = useState(propExpanded || isArray);
const [deepExpand, setDeepExpand] = useState(false);

useEffect(() => {
setDeepExpand(deepExpanded);
if (!isArray) {
setDeepExpand(deepExpanded);
}
}, [deepExpanded, setDeepExpand]);

useEffect(() => {
setExpanded(deepExpand);
if (!isArray) {
setExpanded(deepExpand);
}
}, [deepExpand, setExpanded]);

if (
Expand Down Expand Up @@ -88,7 +94,7 @@ export const Schema: React.FunctionComponent<Props> = ({
<div>
<div className="flex py-2">
<div className={`${onlyTitle ? '' : 'min-w-1/4'} mr-2`}>
{isExpandable && !isCircular ? (
{isExpandable && !isCircular && !isArray ? (
<>
<CollapseButton
onClick={() => setExpanded(prev => !prev)}
Expand Down Expand Up @@ -270,7 +276,7 @@ export const Schema: React.FunctionComponent<Props> = ({
reverse ? 'bg-gray-200' : ''
} ${expanded ? 'block' : 'hidden'}`}
>
<SchemaProperties schema={schema} />
<SchemaProperties schema={schema} isArray={isArray} />
<SchemaItems schema={schema} />

{schema.oneOf() &&
Expand Down Expand Up @@ -368,10 +374,12 @@ export const Schema: React.FunctionComponent<Props> = ({

interface SchemaPropertiesProps {
schema: SchemaInterface;
isArray: boolean;
}

const SchemaProperties: React.FunctionComponent<SchemaPropertiesProps> = ({
schema,
isArray,
}) => {
const properties = schema.properties();
if (properties === undefined || !Object.keys(properties)) {
Expand Down Expand Up @@ -471,17 +479,22 @@ const SchemaItems: React.FunctionComponent<SchemaItemsProps> = ({ schema }) => {
!Array.isArray(items) &&
Object.keys(items.properties() || {}).length
) {
return <SchemaProperties schema={items} />;
return <Schema schema={items} isArray={true} />;
} else if (Array.isArray(items)) {
return (
<>
{items.map((item, idx) => (
<Schema schema={item} schemaName={`${idx + 1} item:`} key={idx} />
<Schema
schema={item}
isArray={true}
schemaName={`${idx + 1} item:`}
key={idx}
/>
))}
</>
);
}
return <Schema schema={items} schemaName="Items:" />;
return <Schema schema={items} isArray={true} schemaName="Items:" />;
};

interface AdditionalItemsProps {
Expand Down
56 changes: 56 additions & 0 deletions library/src/components/__tests__/Schema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,60 @@ describe('Schema component', () => {
expect(screen.getByText('false')).toBeDefined();
});
});

describe('should render arrays', () => {
test('which includes oneOf', async () => {
const schemaModel = new SchemaModel({
title: 'Sets',
type: 'array',
items: {
title: 'Set',
type: 'object',
properties: {
pets: {
title: 'Pets',
type: 'array',
items: {
title: 'Pet',
type: 'object',
discriminator: 'type',
properties: {
type: {
title: 'Pet.Type',
type: 'string',
enum: ['CAT', 'DOG'],
},
},
oneOf: [
{
title: 'Cat',
type: 'object',
properties: {
type: {
const: 'CAT',
},
},
},
{
title: 'Dog',
type: 'object',
properties: {
type: {
const: 'DOG',
},
},
},
],
},
},
},
},
});

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

expect(screen.getByText('Adheres to Cat:')).toBeDefined();
expect(screen.getByText('Or to Dog:')).toBeDefined();
});
});
});
Loading