Resolve any JSON/YAML OpenAPI file and resolve common parameters (#132)

* Fix parsing of OpenAPI when content-type is unknown

* Resolve common parameters

* Format
This commit is contained in:
Samy Pessé
2024-02-01 22:29:28 +01:00
committed by GitHub
parent b5e6673976
commit 260305759d
2 changed files with 28 additions and 9 deletions
@@ -49,15 +49,30 @@ export async function fetchOpenAPIOperation<Markdown>(
): Promise<OpenAPIOperationData | null> {
const fetcher = cacheFetcher(rawFetcher);
const operation = await resolveOpenAPI<OpenAPIV3.OperationObject>(
let operation = await resolveOpenAPI<OpenAPIV3.OperationObject>(
input.url,
['paths', input.path, input.method],
fetcher,
);
if (!operation) {
return null;
}
// Resolve common parameters
const commonParameters = await resolveOpenAPI<OpenAPIV3.ParameterObject[]>(
input.url,
['paths', input.path, 'parameters'],
fetcher,
);
if (commonParameters) {
operation = {
...operation,
parameters: [...commonParameters, ...(operation.parameters ?? [])],
};
}
// Resolve servers
const servers = await resolveOpenAPI<OpenAPIV3.ServerObject[]>(input.url, ['servers'], fetcher);
// Resolve securities
+12 -8
View File
@@ -50,15 +50,19 @@ const fetcher: OpenAPIFetcher = {
let data: unknown = null;
if (response.ok) {
if (response.headers.get('content-type')?.includes('yaml')) {
const text = await response.text();
// Try with JSON
try {
data = JSON.parse(text);
} catch (error) {
//
}
// Try with YAML
if (!data) {
try {
data = yaml.load(await response.text());
} catch (error) {
//
}
} else if (response.headers.get('content-type')?.includes('json')) {
try {
data = await response.json();
data = yaml.load(text);
} catch (error) {
//
}