From 977e42f0e7205f23dbf8e8bb38bd73a72463e427 Mon Sep 17 00:00:00 2001 From: acethecreator Date: Tue, 11 Mar 2025 18:42:43 +0100 Subject: [PATCH 1/5] fix multischema schema level in payload --- library/src/containers/Messages/MessageExample.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/src/containers/Messages/MessageExample.tsx b/library/src/containers/Messages/MessageExample.tsx index d2fcb7c63..a25aefd04 100644 --- a/library/src/containers/Messages/MessageExample.tsx +++ b/library/src/containers/Messages/MessageExample.tsx @@ -54,9 +54,15 @@ export const Example: React.FunctionComponent = ({ const [expanded, setExpanded] = useState( config?.expand?.messageExamples ?? false, ); - useEffect(() => { setExpanded(config?.expand?.messageExamples ?? false); + // Detect multi-format schema and restructure the schema property + // to ensure compatibility with the sample generator + const schemaData = schema.json(); + if (schemaData.schema) { + Object.assign(schemaData, schemaData.schema); + delete schemaData.schema; + } // eslint-disable-next-line react-hooks/exhaustive-deps }, [config.expand]); From 1175fe9e4df26337dfd59aa924d0171a5985ea00 Mon Sep 17 00:00:00 2001 From: acethecreator Date: Tue, 11 Mar 2025 19:08:47 +0100 Subject: [PATCH 2/5] fixed type safety issue --- library/src/containers/Messages/MessageExample.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/library/src/containers/Messages/MessageExample.tsx b/library/src/containers/Messages/MessageExample.tsx index a25aefd04..961dd5182 100644 --- a/library/src/containers/Messages/MessageExample.tsx +++ b/library/src/containers/Messages/MessageExample.tsx @@ -10,6 +10,18 @@ interface Props { message: MessageInterface; } +interface SchemaWithFormat { + schemaFormat: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + schema: any; +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function isMultiFormatSchema(schema: any): schema is SchemaWithFormat { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return schema; +} + export const MessageExample: React.FunctionComponent = ({ message }) => { if (!message) { return null; @@ -59,7 +71,7 @@ export const Example: React.FunctionComponent = ({ // Detect multi-format schema and restructure the schema property // to ensure compatibility with the sample generator const schemaData = schema.json(); - if (schemaData.schema) { + if (isMultiFormatSchema(schemaData)) { Object.assign(schemaData, schemaData.schema); delete schemaData.schema; } From 1a71ab43bf87c4f59dd98625c55d182dccc4f061 Mon Sep 17 00:00:00 2001 From: acethecreator Date: Tue, 11 Mar 2025 19:11:22 +0100 Subject: [PATCH 3/5] retrun valid type --- library/src/containers/Messages/MessageExample.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/src/containers/Messages/MessageExample.tsx b/library/src/containers/Messages/MessageExample.tsx index 961dd5182..adb09e0e8 100644 --- a/library/src/containers/Messages/MessageExample.tsx +++ b/library/src/containers/Messages/MessageExample.tsx @@ -19,7 +19,12 @@ interface SchemaWithFormat { // eslint-disable-next-line @typescript-eslint/no-explicit-any function isMultiFormatSchema(schema: any): schema is SchemaWithFormat { // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return schema; + return ( + schema && + typeof schema === 'object' && + 'schemaFormat' in schema && + 'schema' in schema + ); } export const MessageExample: React.FunctionComponent = ({ message }) => { From 5e01ba164776602857f2575c51e6f5e3e8a86f2d Mon Sep 17 00:00:00 2001 From: acethecreator Date: Sat, 2 May 2026 19:25:57 +0100 Subject: [PATCH 4/5] fix: move message multi scheam logic to message helper --- .../containers/Messages/MessageExample.tsx | 24 ------------------- library/src/helpers/message.ts | 10 +++++++- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/library/src/containers/Messages/MessageExample.tsx b/library/src/containers/Messages/MessageExample.tsx index adb09e0e8..37dc89016 100644 --- a/library/src/containers/Messages/MessageExample.tsx +++ b/library/src/containers/Messages/MessageExample.tsx @@ -10,23 +10,6 @@ interface Props { message: MessageInterface; } -interface SchemaWithFormat { - schemaFormat: string; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - schema: any; -} - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -function isMultiFormatSchema(schema: any): schema is SchemaWithFormat { - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return ( - schema && - typeof schema === 'object' && - 'schemaFormat' in schema && - 'schema' in schema - ); -} - export const MessageExample: React.FunctionComponent = ({ message }) => { if (!message) { return null; @@ -73,13 +56,6 @@ export const Example: React.FunctionComponent = ({ ); useEffect(() => { setExpanded(config?.expand?.messageExamples ?? false); - // Detect multi-format schema and restructure the schema property - // to ensure compatibility with the sample generator - const schemaData = schema.json(); - if (isMultiFormatSchema(schemaData)) { - Object.assign(schemaData, schemaData.schema); - delete schemaData.schema; - } // eslint-disable-next-line react-hooks/exhaustive-deps }, [config.expand]); diff --git a/library/src/helpers/message.ts b/library/src/helpers/message.ts index aa9bc487d..2501a310d 100644 --- a/library/src/helpers/message.ts +++ b/library/src/helpers/message.ts @@ -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 ''; } From 9446fbdbb87ec336014dca3512754f1c090a2e67 Mon Sep 17 00:00:00 2001 From: acethecreator Date: Sat, 2 May 2026 19:28:28 +0100 Subject: [PATCH 5/5] fix: add multischema test --- library/src/helpers/__tests__/message.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/src/helpers/__tests__/message.test.ts b/library/src/helpers/__tests__/message.test.ts index 55f5e960c..e3e61aa6c 100644 --- a/library/src/helpers/__tests__/message.test.ts +++ b/library/src/helpers/__tests__/message.test.ts @@ -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', () => {