Skip to content

render default value as string literal instead of markdown #958

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

Merged
merged 6 commits into from
Sep 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,16 @@ function createDetailsNode(
create("div", {
style: { marginLeft: "1rem" },
children: [
guard(getQualifierMessage(schema), (message) =>
guard(schema.description, (description) =>
create("div", {
style: { marginTop: ".5rem", marginBottom: ".5rem" },
children: createDescription(message),
children: createDescription(description),
})
),
guard(schema.description, (description) =>
guard(getQualifierMessage(schema), (message) =>
create("div", {
style: { marginTop: ".5rem", marginBottom: ".5rem" },
children: createDescription(description),
children: createDescription(message),
})
),
createNodes(schema, SCHEMA_TYPE),
Expand Down Expand Up @@ -559,20 +559,20 @@ function createPropertyDiscriminator(
]),
],
}),
guard(getQualifierMessage(discriminator), (message) =>
guard(schema.description, (description) =>
create("div", {
style: {
paddingLeft: "1rem",
},
children: createDescription(message),
children: createDescription(description),
})
),
guard(schema.description, (description) =>
guard(getQualifierMessage(discriminator), (message) =>
create("div", {
style: {
paddingLeft: "1rem",
},
children: createDescription(description),
children: createDescription(message),
})
),
create("DiscriminatorTabs", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ function ParamsItem({ param, ...rest }: Props) {
} = param;

let schema = param.schema;
let defaultValue: string | undefined;

if (!schema || !schema?.type) {
schema = { type: "any" };
}
if (schema) {
if (schema.items) {
defaultValue = schema.items.default;
} else {
defaultValue = schema.default;
}
}

const renderSchemaName = guard(schema, (schema) => (
<span className="openapi-schema__type"> {getSchemaName(schema)}</span>
Expand Down Expand Up @@ -131,18 +139,29 @@ function ParamsItem({ param, ...rest }: Props) {
}
);

const renderDefaultValue = guard(
schema && schema.items
? schema.items.default
: schema
? schema.default
: undefined,
(value) => (
<div>
<ReactMarkdown children={`**Default value:** \`${value}\``} />
</div>
)
);
function renderDefaultValue() {
if (defaultValue !== undefined) {
if (typeof defaultValue === "string") {
return (
<div>
<strong>Default value: </strong>
<span>
<code>{defaultValue}</code>
</span>
</div>
);
}
return (
<div>
<strong>Default value: </strong>
<span>
<code>{JSON.stringify(defaultValue)}</code>
</span>
</div>
);
}
return undefined;
}

const renderExample = guard(toString(example), (example) => (
<div>
Expand Down Expand Up @@ -196,9 +215,9 @@ function ParamsItem({ param, ...rest }: Props) {
{renderDeprecated}
</span>
{renderSchema}
{renderDefaultValue}
{renderDescription}
{renderEnumDescriptions}
{renderDefaultValue()}
{renderExample}
{renderExamples}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function SchemaItem(props: Props) {
} = props;
let deprecated;
let schemaDescription;
let defaultValue;
let defaultValue: string | undefined;
let nullable;
let enumDescriptions: [string, string][] = [];

Expand Down Expand Up @@ -133,11 +133,29 @@ export default function SchemaItem(props: Props) {
</div>
));

const renderDefaultValue = guard(defaultValue, (value) => (
<div className="">
<ReactMarkdown children={`**Default value:** \`${value}\``} />
</div>
));
function renderDefaultValue() {
if (defaultValue !== undefined) {
if (typeof defaultValue === "string") {
return (
<div>
<strong>Default value: </strong>
<span>
<code>{defaultValue}</code>
</span>
</div>
);
}
return (
<div>
<strong>Default value: </strong>
<span>
<code>{JSON.stringify(defaultValue)}</code>
</span>
</div>
);
}
return undefined;
}

const schemaContent = (
<div>
Expand All @@ -157,10 +175,10 @@ export default function SchemaItem(props: Props) {
{renderRequired}
{renderDeprecated}
</span>
{renderQualifierMessage}
{renderDefaultValue}
{renderSchemaDescription}
{renderEnumDescriptions}
{renderQualifierMessage}
{renderDefaultValue()}
{collapsibleSchemaContent ?? collapsibleSchemaContent}
</div>
);
Expand Down
Loading
Loading