Skip to content

Commit

Permalink
Merge pull request #958 from PaloAltoNetworks/default-values
Browse files Browse the repository at this point in the history
render default value as string literal instead of markdown
  • Loading branch information
sserrata authored Sep 10, 2024
2 parents dca70f4 + 8323e7d commit 3dc71cd
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 32 deletions.
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

0 comments on commit 3dc71cd

Please sign in to comment.