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.

utils.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 { mapValues } from 'lodash';
  21. import { OpenAPIV3 } from 'openapi-types';
  22. import { isDefined } from '../../helpers/types';
  23. import { DereferenceRecursive, ExcludeReferences } from './types';
  24. export const URL_DIVIDER = '--';
  25. type ConvertedSchema = string | { [Key: string]: ConvertedSchema } | ConvertedSchema[];
  26. export const mapOpenAPISchema = (
  27. schema: ExcludeReferences<OpenAPIV3.SchemaObject>,
  28. ): ConvertedSchema => {
  29. if (schema.type === 'object') {
  30. const result = { ...schema.properties };
  31. return mapValues(result, (schema) => mapOpenAPISchema(schema)) as ConvertedSchema;
  32. }
  33. if (schema.type === 'array') {
  34. return [mapOpenAPISchema(schema.items)];
  35. }
  36. if (schema.enum) {
  37. return `Enum (${schema.type}): ${(schema.enum as ConvertedSchema[]).join(', ')}`;
  38. }
  39. if (schema.format) {
  40. return `${schema.type} (${schema.format})`;
  41. }
  42. return schema.type as string;
  43. };
  44. export const dereferenceSchema = (
  45. document: OpenAPIV3.Document,
  46. ): ExcludeReferences<OpenAPIV3.Document> => {
  47. const dereference = (ref: string) => {
  48. const path = ref.replace('#/', '').split('/');
  49. return path.reduce((acc: any, key) => {
  50. if (key in acc) {
  51. return acc[key];
  52. }
  53. return {};
  54. }, document);
  55. };
  56. const dereferenceRecursive = <P>(val: P | OpenAPIV3.ReferenceObject): DereferenceRecursive<P> => {
  57. if (typeof val === 'object' && val !== null) {
  58. if ('$ref' in val) {
  59. return dereferenceRecursive(dereference(val.$ref));
  60. } else if (Array.isArray(val)) {
  61. return val.map(dereferenceRecursive) as DereferenceRecursive<P>;
  62. }
  63. return mapValues(val, dereferenceRecursive) as DereferenceRecursive<P>;
  64. }
  65. return val as DereferenceRecursive<P>;
  66. };
  67. return dereferenceRecursive(document) as ExcludeReferences<OpenAPIV3.Document>;
  68. };
  69. export const getResponseCodeClassName = (code: string): string => {
  70. switch (code[0]) {
  71. case '1':
  72. return 'sw-bg-blue-200';
  73. case '2':
  74. return 'sw-bg-green-200';
  75. case '3':
  76. return 'sw-bg-yellow-200';
  77. case '4':
  78. case '5':
  79. return 'sw-bg-red-200';
  80. default:
  81. return 'sw-bg-gray-200';
  82. }
  83. };
  84. export const getApiEndpointKey = (name: string, method: string) => `${name}${URL_DIVIDER}${method}`;
  85. const DISPLAYED_MEDIA_TYPES = ['application/json', 'application/merge-patch+json'];
  86. export function extractSchemaAndMediaType(
  87. content?: Exclude<ExcludeReferences<OpenAPIV3.ResponseObject>['content'], undefined>,
  88. ) {
  89. if (!content) {
  90. return [];
  91. }
  92. const requests = Object.keys(content)
  93. .filter((mediaType) => DISPLAYED_MEDIA_TYPES.includes(mediaType))
  94. .map((requestMediaType) => {
  95. const schema = content[requestMediaType]?.schema;
  96. if (!schema) {
  97. return null;
  98. }
  99. return {
  100. requestMediaType,
  101. schema: JSON.stringify(mapOpenAPISchema(schema), null, 2),
  102. };
  103. })
  104. .filter(isDefined);
  105. return requests;
  106. }