From 936177499319f65e630c23925a374c2e4530cbf0 Mon Sep 17 00:00:00 2001 From: Mark Reynolds Date: Tue, 6 May 2025 20:48:56 -0400 Subject: [PATCH] feat: support const for OAS 3.1.1 --- demo/examples/tests/const.yaml | 93 +++++++++++++++++++ .../src/theme/Schema/index.tsx | 5 +- .../src/theme/SchemaItem/index.tsx | 27 ++++++ 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 demo/examples/tests/const.yaml diff --git a/demo/examples/tests/const.yaml b/demo/examples/tests/const.yaml new file mode 100644 index 000000000..9d03a31fb --- /dev/null +++ b/demo/examples/tests/const.yaml @@ -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 diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx index b1cbda341..1694f5c6f 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx @@ -122,9 +122,10 @@ const AnyOneOf: React.FC = ({ schema, schemaType }) => { value={`${index}-item-properties`} > {/* Handle primitive types directly */} - {["string", "number", "integer", "boolean"].includes( + {(["string", "number", "integer", "boolean"].includes( anyOneSchema.type - ) && ( + ) || + anyOneSchema.const) && ( + Constant value: + + {constValue} + + + ); + } + return ( +
+ Constant value: + + {JSON.stringify(constValue)} + +
+ ); + } + return undefined; + } + const schemaContent = (
@@ -184,6 +210,7 @@ export default function SchemaItem(props: Props) { {renderSchemaDescription} {renderEnumDescriptions} {renderQualifierMessage} + {renderConstValue()} {renderDefaultValue()} {renderExample()} {collapsibleSchemaContent ?? collapsibleSchemaContent}