Skip to content

feat: support const for OAS 3.1.1 #1143

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 1 commit into from
May 9, 2025
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
93 changes: 93 additions & 0 deletions demo/examples/tests/const.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
openapi: 3.1.1
info:
title: Const
description: Demonstrates various const.
version: 1.0.0
tags:
- name: const
description: const tests
paths:
/const:
get:
tags:
- const
summary: const with primitives
description: |
Schema:
```yaml
type: object
properties:
type:
type: string
const: example
title: Example
description: |
This is an example
quality:
type: string
oneOf:
- const: good
title: Good
description: |
This is a good example
- const: bad
title: Bad
description: |
This is a bad example
tags:
type: array
items:
anyOf:
- const: dog
title: Dog
description: |
This is a dog
- const: cat
title: Cat
description: |
This is a cat
required:
- type
- quality
```
responses:
"200":
description: Successful response
content:
application/json:
schema:
type: object
properties:
type:
type: string
const: constExample
title: Const Example
description: |
Const example description
quality:
type: string
oneOf:
- const: good
title: Good
description: |
This is a good example
- const: bad
title: Bad
description: |
This is a bad example
tags:
type: array
items:
type: string
anyOf:
- const: dog
title: Dog
description: |
This is a dog
- const: cat
title: Cat
description: |
This is a cat
required:
- type
- quality
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ const AnyOneOf: React.FC<SchemaProps> = ({ schema, schemaType }) => {
value={`${index}-item-properties`}
>
{/* Handle primitive types directly */}
{["string", "number", "integer", "boolean"].includes(
{(["string", "number", "integer", "boolean"].includes(
anyOneSchema.type
) && (
) ||
anyOneSchema.const) && (
<SchemaItem
collapsible={false}
name={undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function SchemaItem(props: Props) {
let example: string | undefined;
let nullable;
let enumDescriptions: [string, string][] = [];
let constValue: string | undefined;

if (schema) {
deprecated = schema.deprecated;
Expand All @@ -75,6 +76,7 @@ export default function SchemaItem(props: Props) {
nullable =
schema.nullable ||
(Array.isArray(schema.type) && schema.type.includes("null")); // support JSON Schema nullable
constValue = schema.const;
}

const renderRequired = guard(
Expand Down Expand Up @@ -161,6 +163,30 @@ export default function SchemaItem(props: Props) {
return undefined;
}

function renderConstValue() {
if (constValue !== undefined) {
if (typeof constValue === "string") {
return (
<div>
<strong>Constant value: </strong>
<span>
<code>{constValue}</code>
</span>
</div>
);
}
return (
<div>
<strong>Constant value: </strong>
<span>
<code>{JSON.stringify(constValue)}</code>
</span>
</div>
);
}
return undefined;
}

const schemaContent = (
<div>
<span className="openapi-schema__container">
Expand All @@ -184,6 +210,7 @@ export default function SchemaItem(props: Props) {
{renderSchemaDescription}
{renderEnumDescriptions}
{renderQualifierMessage}
{renderConstValue()}
{renderDefaultValue()}
{renderExample()}
{collapsibleSchemaContent ?? collapsibleSchemaContent}
Expand Down