@@ -276,17 +284,6 @@ export function OpenAPISchemaPresentation(props: OpenAPISchemaPropertyEntry) {
* Get the sub-properties of a schema.
*/
function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISchemaPropertyEntry[] {
- if (schema.allOf) {
- return schema.allOf.reduce((acc, subSchema) => {
- const properties = getSchemaProperties(subSchema) ?? [
- {
- schema: subSchema,
- },
- ];
- return [...acc, ...properties];
- }, [] as OpenAPISchemaPropertyEntry[]);
- }
-
// check array AND schema.items as this is sometimes null despite what the type indicates
if (schema.type === 'array' && !!schema.items) {
const items = schema.items;
@@ -295,6 +292,11 @@ function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISche
return itemProperties;
}
+ // If the items are a primitive type, we don't need to display them
+ if (['string', 'number', 'boolean', 'integer'].includes(items.type) && !items.enum) {
+ return null;
+ }
+
return [
{
propertyName: 'items',
@@ -351,8 +353,7 @@ export function getSchemaAlternatives(
}
if (schema.allOf) {
- // allOf is managed in `getSchemaProperties`
- return null;
+ return [flattenAlternatives('allOf', schema.allOf, downAncestors), schema.discriminator];
}
return null;
@@ -378,11 +379,6 @@ export function getSchemaTitle(
/** If the title is inferred in a oneOf with discriminator, we can use it to optimize the title */
discriminator?: OpenAPIV3.DiscriminatorObject,
): string {
- if (schema.title) {
- // If the schema has a title, use it
- return schema.title;
- }
-
// Try using the discriminator
if (discriminator?.propertyName && schema.properties) {
const discriminatorProperty = schema.properties[discriminator.propertyName];
@@ -419,21 +415,22 @@ export function getSchemaTitle(
type = 'not';
}
- if (schema.minimum || schema.minLength) {
- type += ` · min: ${schema.minimum || schema.minLength}`;
- }
-
- if (schema.maximum || schema.maxLength) {
- type += ` · max: ${schema.maximum || schema.maxLength}`;
- }
-
- if (schema.default) {
- type += ` · default: ${schema.default}`;
- }
-
- if (schema.nullable) {
- type = `${type} | nullable`;
- }
-
return type;
}
+
+function getDisclosureLabel(schema: OpenAPIV3.SchemaObject): string | undefined {
+ if (schema.type === 'array' && !!schema.items) {
+ if (schema.items.oneOf) {
+ return 'available items';
+ }
+
+ // Fallback to "child attributes" for enums and objects
+ if (schema.items.enum || schema.items.type === 'object') {
+ return;
+ }
+
+ return schema.items.title ?? schema.title ?? getSchemaTitle(schema.items);
+ }
+
+ return schema.title;
+}
diff --git a/packages/react-openapi/src/OpenAPISchemaName.tsx b/packages/react-openapi/src/OpenAPISchemaName.tsx
index c3ff40e26..bab615823 100644
--- a/packages/react-openapi/src/OpenAPISchemaName.tsx
+++ b/packages/react-openapi/src/OpenAPISchemaName.tsx
@@ -1,8 +1,10 @@
+import { OpenAPIV3 } from '@gitbook/openapi-parser';
+
interface OpenAPISchemaNameProps {
+ schema?: OpenAPIV3.SchemaObject;
propertyName?: string | JSX.Element;
required?: boolean;
type?: string;
- deprecated?: boolean;
}
/**
@@ -10,18 +12,48 @@ interface OpenAPISchemaNameProps {
* It includes the property name, type, required and deprecated status.
*/
export function OpenAPISchemaName(props: OpenAPISchemaNameProps): JSX.Element {
- const { type, propertyName, required, deprecated } = props;
+ const { schema, type, propertyName, required } = props;
+
+ const additionalItems = schema && getAdditionalItems(schema);
return (
{propertyName ? (
-
+
{propertyName}
) : null}
- {type ? {type} : null}
+
+ {type ? {type} : null}
+ {additionalItems ? (
+ {additionalItems}
+ ) : null}
+
{required ? required : null}
- {deprecated ? Deprecated : null}
+ {schema?.deprecated ? Deprecated : null}
);
}
+
+function getAdditionalItems(schema: OpenAPIV3.SchemaObject): string {
+ let additionalItems = '';
+
+ if (schema.minimum || schema.minLength) {
+ additionalItems += ` · min: ${schema.minimum || schema.minLength}`;
+ }
+
+ if (schema.maximum || schema.maxLength) {
+ additionalItems += ` · max: ${schema.maximum || schema.maxLength}`;
+ }
+
+ // If the schema has a default value, we display it
+ if (typeof schema.default !== 'undefined') {
+ additionalItems += ` · default: ${schema.default}`;
+ }
+
+ if (schema.nullable) {
+ additionalItems = ` | nullable`;
+ }
+
+ return additionalItems;
+}
diff --git a/packages/react-openapi/src/utils.ts b/packages/react-openapi/src/utils.ts
index 5130c35ab..664bd43f7 100644
--- a/packages/react-openapi/src/utils.ts
+++ b/packages/react-openapi/src/utils.ts
@@ -13,7 +13,11 @@ export function createStateKey(key: string, scope?: string) {
/**
* Resolve the description of an object.
*/
-export function resolveDescription(object: AnyObject) {
+export function resolveDescription(object: OpenAPIV3.SchemaObject | AnyObject) {
+ if ('items' in object && object.items) {
+ return resolveDescription(object.items);
+ }
+
return 'x-gitbook-description-html' in object &&
typeof object['x-gitbook-description-html'] === 'string'
? object['x-gitbook-description-html'].trim()