Skip to content

fix: multischema schema level in message payload #1139

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion library/src/containers/Messages/MessageExample.tsx
Original file line number Diff line number Diff line change
@@ -10,6 +10,23 @@ 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<Props> = ({ message }) => {
if (!message) {
return null;
@@ -54,9 +71,15 @@ export const Example: React.FunctionComponent<ExampleProps> = ({
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 (isMultiFormatSchema(schemaData)) {
Object.assign(schemaData, schemaData.schema);
delete schemaData.schema;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [config.expand]);