Skip to content
Open
1 change: 0 additions & 1 deletion library/src/containers/Messages/MessageExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const Example: React.FunctionComponent<ExampleProps> = ({
const [expanded, setExpanded] = useState(
config?.expand?.messageExamples ?? false,
);

useEffect(() => {
setExpanded(config?.expand?.messageExamples ?? false);
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
18 changes: 18 additions & 0 deletions library/src/helpers/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ describe('MessageHelpers', () => {
b: 0,
});
});

test('should generate example from multi-format schema', () => {
expect(
MessageHelpers.generateExample({
schemaFormat: 'application/vnd.aai.asyncapi+json;version=3.0.0',
schema: {
type: 'object',
properties: {
userId: { type: 'string' },
age: { type: 'integer' },
},
},
}),
).toEqual({
userId: 'string',
age: 0,
});
});
});

describe('.sanitizeExample', () => {
Expand Down
10 changes: 9 additions & 1 deletion library/src/helpers/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ export class MessageHelpers {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static generateExample(schema: any, options: any = {}) {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const resolvedSchema =
schema &&
typeof schema === 'object' &&
'schemaFormat' in schema &&
'schema' in schema
? (schema as { schema: unknown }).schema
: schema;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument
return this.sanitizeExample(sample(schema, options)) || '';
return this.sanitizeExample(sample(resolvedSchema, options)) || '';
} catch (e) {
return '';
}
Expand Down
Loading