import type { OpenAPIV3 } from '@gitbook/openapi-parser'; import type React from 'react'; import type { OpenAPIClientContext } from './context'; import { t, tString } from './translate'; import { getEffectiveArrayType } from './utils'; interface OpenAPISchemaNameProps { schema?: OpenAPIV3.SchemaObject; propertyName?: string | React.JSX.Element; required?: boolean | null; isDiscriminatorProperty?: boolean; type?: string; context: OpenAPIClientContext; } /** * Display the schema name row. * It includes the property name, type, required and deprecated status. */ export function OpenAPISchemaName(props: OpenAPISchemaNameProps) { const { schema, type, propertyName, required, isDiscriminatorProperty, context } = props; const additionalItems = schema && getAdditionalItems(schema, context); return ( {propertyName ? ( {propertyName} ) : null} {isDiscriminatorProperty ? ( {t(context.translation, 'discriminator')} ) : null} {type || additionalItems ? ( {schema?.const ? ( const: {schema?.const} ) : type ? ( {type} ) : null} {additionalItems ? ( {additionalItems} ) : null} ) : null} {schema?.readOnly ? ( {t(context.translation, 'read_only')} ) : null} {schema?.writeOnly ? ( {t(context.translation, 'write_only')} ) : null} {required === null ? null : required ? ( {t(context.translation, 'required')} ) : ( {t(context.translation, 'optional')} )} {schema?.deprecated ? ( {t(context.translation, 'deprecated')} ) : null} ); } function getAdditionalItems(schema: OpenAPIV3.SchemaObject, context: OpenAPIClientContext): string { let additionalItems = ''; if (schema.minimum || schema.minLength || schema.minItems) { additionalItems += ` · ${tString(context.translation, 'min').toLowerCase()}: ${schema.minimum || schema.minLength || schema.minItems}`; } if (schema.maximum || schema.maxLength || schema.maxItems) { additionalItems += ` · ${tString(context.translation, 'max').toLowerCase()}: ${schema.maximum || schema.maxLength || schema.maxItems}`; } // Check for nullable in both OpenAPI 3.0 (nullable: true) and OpenAPI 3.1 (type: ['null', ...]) const schemaArrayInfo = getEffectiveArrayType(schema); const isNullable = schema.nullable || schemaArrayInfo.hasNull; if (isNullable) { additionalItems += ` · ${tString(context.translation, 'nullable').toLowerCase()}`; } return additionalItems; }