/* * SonarQube * Copyright (C) 2009-2023 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { Accordion, Badge, SubHeading, SubTitle, TextMuted } from 'design-system'; import { groupBy } from 'lodash'; import { OpenAPIV3 } from 'openapi-types'; import React from 'react'; import { FormattedMessage } from 'react-intl'; import { translate } from '../../../helpers/l10n'; import { ExcludeReferences } from '../types'; import { mapOpenAPISchema } from '../utils'; import ApiRequestBodyParameters from './ApiRequestParameters'; import ApiResponseSchema from './ApiResponseSchema'; interface Props { data: ExcludeReferences; } export default function ApiParameters({ data }: Props) { const [openParameters, setOpenParameters] = React.useState([]); const toggleParameter = (name: string) => { if (openParameters.includes(name)) { setOpenParameters(openParameters.filter((n) => n !== name)); } else { setOpenParameters([...openParameters, name]); } }; const getSchemaType = (schema: ExcludeReferences) => { const mappedSchema = mapOpenAPISchema(schema); return typeof mappedSchema === 'object' ? JSON.stringify(mappedSchema) : mappedSchema; }; const requestBody = data.requestBody?.content; return ( <> {translate('api_documentation.v2.parameter_header')} {Object.entries(groupBy(data.parameters, (p) => p.in)).map(([group, parameters]) => (
{translate(`api_documentation.v2.request_subheader.${group}`)}
    {parameters.map((parameter) => { return ( {parameter.name}{' '} {parameter.schema && ( )} {parameter.required && ( {translate('required')} )} {parameter.deprecated && ( {translate('deprecated')} )}
} data={parameter.name} onClick={toggleParameter} open={openParameters.includes(parameter.name)} >
{parameter.description}
{parameter.schema?.enum && (
{parameter.schema.enum.join(', ')}
), }} /> )} {parameter.schema?.maximum && ( )} {typeof parameter.schema?.minimum === 'number' && ( )} {parameter.example !== undefined && ( )} {parameter.schema?.default !== undefined && ( )} ); })} ))} {!requestBody && !data.parameters?.length && } {requestBody && (
{translate('api_documentation.v2.request_subheader.request_body')}
)} ); }