Skip to content

Migrate from openapi-parser to openapi-resolver #948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/specs/oa-3.1.json
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
"schemas": {
"options": {
"type": "object",
"description": "Options",
"properties": {
"const": { "type": "string", "const": "a_fixed_value" },
"enum": { "type": "string", "enum": ["a_single_enum_value"] },
@@ -27,7 +28,10 @@
"description": "The custom fields collection",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/options" }
"schema": {
"$ref": "#/components/schemas/options",
"description": "Options Description Override"
}
}
}
},
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
"module": "dist/rapidoc-min.js",
"mode": "production",
"dependencies": {
"@apitools/openapi-parser": "0.0.32",
"openapi-resolver": "^4.1.35",
"base64-arraybuffer": "^1.0.2",
"buffer": "^6.0.3",
"lit": "^2.7.5",
13 changes: 9 additions & 4 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
@@ -390,6 +390,9 @@ function addPropertyExampleToObjectExamples(example, obj, propertyKey) {
}

function mergePropertyExamples(obj, propertyName, propExamples) {
if (propExamples == null || Object.keys(propExamples).length === 0) {
return obj;
}
// Create an example for each variant of the propertyExample, merging them with the current (parent) example
let i = 0;
const maxCombinations = 10;
@@ -422,11 +425,11 @@ export function schemaToSampleObj(schema, config = { }) {
if (schema.allOf.length === 1 && !schema.allOf[0]?.properties && !schema.allOf[0]?.items) {
// If allOf has single item and the type is not an object or array, then its a primitive
if (schema.allOf[0].$ref) {
return '{ }';
return { 'example-0': '{ }' };
}
if (schema.allOf[0].readOnly && config.includeReadOnly) {
const tempSchema = schema.allOf[0];
return getSampleValueByType(tempSchema);
return { 'example-0': getSampleValueByType(tempSchema) };
}
return;
}
@@ -573,8 +576,10 @@ export function schemaToSampleObj(schema, config = { }) {
if (config.useXmlTagForProp) {
const xmlTagName = schema.properties[propertyName].xml?.name || propertyName;
if (schema.properties[propertyName].xml?.wrapped) {
const wrappedItemSample = JSON.parse(`{ "${xmlTagName}" : { "${xmlTagName}" : ${JSON.stringify(itemSamples['example-0'])} } }`);
obj = mergePropertyExamples(obj, xmlTagName, wrappedItemSample);
try {
const wrappedItemSample = JSON.parse(`{ "${xmlTagName}" : { "${xmlTagName}" : ${JSON.stringify(itemSamples['example-0'])} } }`);
obj = mergePropertyExamples(obj, xmlTagName, wrappedItemSample);
} catch (e) { /* ignore */ }
} else {
obj = mergePropertyExamples(obj, xmlTagName, itemSamples);
}
49 changes: 14 additions & 35 deletions src/utils/spec-parser.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,29 @@
/* eslint-disable no-use-before-define */
import OpenApiParser from '@apitools/openapi-parser';
import OpenApiResolver from 'openapi-resolver/dist/openapi-resolver.browser';
import { marked } from 'marked';
import { invalidCharsRegEx, rapidocApiKey, sleep } from '~/utils/common-utils';

export default async function ProcessSpec(specUrl, generateMissingTags = false, sortTags = false, sortEndpointsBy = '', attrApiKey = '', attrApiKeyLocation = '', attrApiKeyValue = '', serverUrl = '') {
let jsonParsedSpec;
try {
this.requestUpdate(); // important to show the initial loader
let specMeta;
if (typeof specUrl === 'string') {
specMeta = await OpenApiParser.resolve({ url: specUrl, allowMetaPatches: false }); // Swagger(specUrl);
} else {
specMeta = await OpenApiParser.resolve({ spec: specUrl, allowMetaPatches: false }); // Swagger({ spec: specUrl });
}
jsonParsedSpec = await OpenApiResolver.resolve(specUrl);

await sleep(0); // important to show the initial loader (allows for rendering updates)

// If JSON Schema Viewer
if (specMeta.resolvedSpec?.jsonSchemaViewer && specMeta.resolvedSpec?.schemaAndExamples) {
this.dispatchEvent(new CustomEvent('before-render', { detail: { spec: specMeta.resolvedSpec } }));
const schemaAndExamples = Object.entries(specMeta.resolvedSpec.schemaAndExamples).map((v) => ({ show: true, expanded: true, selectedExample: null, name: v[0], elementId: v[0].replace(invalidCharsRegEx, '-'), ...v[1] }));
const parsedSpec = {
specLoadError: false,
isSpecLoading: false,
info: specMeta.resolvedSpec.info,
schemaAndExamples,
};
return parsedSpec;
}
if (specMeta.spec && (specMeta.spec.components || specMeta.spec.info || specMeta.spec.servers || specMeta.spec.tags || specMeta.spec.paths)) {
jsonParsedSpec = specMeta.spec;
this.dispatchEvent(new CustomEvent('before-render', { detail: { spec: jsonParsedSpec } }));
} else {
console.info('RapiDoc: %c There was an issue while parsing the spec %o ', 'color:orangered', specMeta); // eslint-disable-line no-console
return {
specLoadError: true,
isSpecLoading: false,
info: {
title: 'Error loading the spec',
description: specMeta.response?.url ? `${specMeta.response?.url} ┃ ${specMeta.response?.status} ${specMeta.response?.statusText}` : 'Unable to load the Spec',
version: ' ',
},
tags: [],
};
}
this.dispatchEvent(new CustomEvent('before-render', { detail: { spec: jsonParsedSpec } }));
} catch (err) {
console.info('RapiDoc: %c There was an issue while parsing the spec %o ', 'color:orangered', err); // eslint-disable-line no-console
return {
specLoadError: true,
isSpecLoading: false,
info: {
title: 'Error loading the spec',
description: err.message || 'Unable to load the Spec',
version: ' ',
},
tags: [],
};
}

// const pathGroups = groupByPaths(jsonParsedSpec);
Loading