You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApiParameters.tsx 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { Accordion, Badge, SubHeading, SubTitle, TextMuted } from 'design-system';
  21. import { groupBy } from 'lodash';
  22. import { OpenAPIV3 } from 'openapi-types';
  23. import React, { useContext } from 'react';
  24. import { FormattedMessage } from 'react-intl';
  25. import { translate } from '../../../helpers/l10n';
  26. import { ExcludeReferences, InternalExtension } from '../types';
  27. import { mapOpenAPISchema } from '../utils';
  28. import ApiFilterContext from './ApiFilterContext';
  29. import ApiRequestBodyParameters from './ApiRequestParameters';
  30. import ApiRequestSchema from './ApiRequestSchema';
  31. interface Props {
  32. data: ExcludeReferences<OpenAPIV3.OperationObject>;
  33. }
  34. export default function ApiParameters({ data }: Readonly<Props>) {
  35. const [openParameters, setOpenParameters] = React.useState<string[]>([]);
  36. const { showInternal } = useContext(ApiFilterContext);
  37. const toggleParameter = (name: string) => {
  38. if (openParameters.includes(name)) {
  39. setOpenParameters(openParameters.filter((n) => n !== name));
  40. } else {
  41. setOpenParameters([...openParameters, name]);
  42. }
  43. };
  44. const getSchemaType = (schema: ExcludeReferences<OpenAPIV3.SchemaObject>) => {
  45. const mappedSchema = mapOpenAPISchema(schema);
  46. return typeof mappedSchema === 'object' ? JSON.stringify(mappedSchema) : mappedSchema;
  47. };
  48. const requestBody = data.requestBody?.content;
  49. return (
  50. <>
  51. <SubTitle>{translate('api_documentation.v2.parameter_header')}</SubTitle>
  52. {Object.entries(groupBy(data.parameters, (p) => p.in)).map(
  53. ([group, parameters]: [
  54. string,
  55. ExcludeReferences<Array<OpenAPIV3.ParameterObject & InternalExtension>>,
  56. ]) => (
  57. <div key={group}>
  58. <SubHeading id={`api-parameters-${group}`}>
  59. {translate(`api_documentation.v2.request_subheader.${group}`)}
  60. </SubHeading>
  61. <ul aria-labelledby={`api-parameters-${group}`}>
  62. {parameters
  63. .filter((parameter) => showInternal || !parameter['x-internal'])
  64. .map((parameter) => {
  65. return (
  66. <Accordion
  67. className="sw-mt-2 sw-mb-4"
  68. key={parameter.name}
  69. header={
  70. <div>
  71. {parameter.name}{' '}
  72. {parameter.schema && (
  73. <TextMuted
  74. className="sw-inline sw-ml-2"
  75. text={getSchemaType(parameter.schema)}
  76. />
  77. )}
  78. {parameter.required && (
  79. <Badge className="sw-ml-2">{translate('required')}</Badge>
  80. )}
  81. {parameter.deprecated && (
  82. <Badge variant="deleted" className="sw-ml-2">
  83. {translate('deprecated')}
  84. </Badge>
  85. )}
  86. {parameter['x-internal'] && (
  87. <Badge variant="new" className="sw-ml-2">
  88. {translate('internal')}
  89. </Badge>
  90. )}
  91. </div>
  92. }
  93. data={parameter.name}
  94. onClick={toggleParameter}
  95. open={openParameters.includes(parameter.name)}
  96. >
  97. <div>{parameter.description}</div>
  98. {parameter.schema?.enum && (
  99. <div className="sw-mt-2">
  100. <FormattedMessage
  101. id="api_documentation.v2.enum_description"
  102. values={{
  103. values: (
  104. <div className="sw-body-sm-highlight">
  105. {parameter.schema.enum.join(', ')}
  106. </div>
  107. ),
  108. }}
  109. />
  110. </div>
  111. )}
  112. {parameter.schema?.maximum && (
  113. <TextMuted
  114. className="sw-mt-2 sw-block"
  115. text={`${translate('max')}: ${parameter.schema?.maximum}`}
  116. />
  117. )}
  118. {typeof parameter.schema?.minimum === 'number' && (
  119. <TextMuted
  120. className="sw-mt-2 sw-block"
  121. text={`${translate('min')}: ${parameter.schema?.minimum}`}
  122. />
  123. )}
  124. {parameter.example !== undefined && (
  125. <TextMuted
  126. className="sw-mt-2 sw-block"
  127. text={`${translate('example')}: ${parameter.example}`}
  128. />
  129. )}
  130. {parameter.schema?.default !== undefined && (
  131. <TextMuted
  132. className="sw-mt-2 sw-block"
  133. text={`${translate('default')}: ${parameter.schema?.default}`}
  134. />
  135. )}
  136. </Accordion>
  137. );
  138. })}
  139. </ul>
  140. </div>
  141. ),
  142. )}
  143. {!requestBody && !data.parameters?.length && <TextMuted text={translate('no_data')} />}
  144. {requestBody && (
  145. <div>
  146. <SubHeading id="api_documentation.v2.request_subheader.request_body">
  147. {translate('api_documentation.v2.request_subheader.request_body')}
  148. </SubHeading>
  149. <ApiRequestSchema content={requestBody} />
  150. <ApiRequestBodyParameters content={requestBody} />
  151. </div>
  152. )}
  153. </>
  154. );
  155. }