Skip to content
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

fix: Correct pathing in OpenApi Parser Printing #2032

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { camelCase } from "es-toolkit";
import { OpenAPIV3_1 } from "openapi-types";
import { v4 } from "uuid";
import { FernRegistry } from "../../client/generated";
import { SubpackageId } from "../../client/generated/api/resources/api/resources/v1";
import {
BaseOpenApiV3_1ConverterNode,
BaseOpenApiV3_1ConverterNodeConstructorArgs,
Expand Down Expand Up @@ -70,7 +69,7 @@ export class OpenApiDocumentConverterNode extends BaseOpenApiV3_1ConverterNode<
input: tag,
context: this.context,
accessPath: this.accessPath,
pathId: `tags[${index}]`,
pathId: ["tags", `${index}`],
})
);
}
Expand Down Expand Up @@ -143,8 +142,9 @@ export class OpenApiDocumentConverterNode extends BaseOpenApiV3_1ConverterNode<
const apiDefinitionId = v4();

const { webhookEndpoints, endpoints } = this.paths?.convert() ?? {};

const subpackages: Record<
SubpackageId,
FernRegistry.api.v1.SubpackageId,
FernRegistry.api.latest.SubpackageMetadata
> = {};
if (endpoints != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class SecurityRequirementObjectConverterNode extends BaseOpenApiV3_1Conve
input: resolvedSecurityScheme,
context: this.context,
accessPath: this.accessPath,
pathId: `security[${index}]`,
pathId: ["security", `${index}`],
});

if (resolvedAuthScheme.convert() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ export class RedocExampleConverterNode extends BaseOpenApiV3_1ConverterNode<

this.codeSamples.forEach((codeSample) => {
if (
Object.values(FernRegistry.api.v1.read.SupportedLanguage).includes(
codeSample.lang.toLowerCase() as FernRegistry.api.v1.read.SupportedLanguage
)
![
...Object.values(FernRegistry.api.v1.read.SupportedLanguage),
"Kotlin",
"Swift",
"PHP",
]
.map((l) => l.toLowerCase())
.includes(codeSample.lang.toLowerCase())
) {
this.context.errors.warning({
message: `Unsupported language: ${codeSample.lang}. This may not render correctly.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from "../../BaseOpenApiV3_1Converter.node";
import { coalesceServers } from "../../utils/3.1/coalesceServers";
import { resolveParameterReference } from "../../utils/3.1/resolveParameterReference";
import { dedupPayloads } from "../../utils/dedupPayloads";
import { getEndpointId } from "../../utils/getEndpointId";
import { SecurityRequirementObjectConverterNode } from "../auth/SecurityRequirementObjectConverter.node";
import { AvailabilityConverterNode } from "../extensions/AvailabilityConverter.node";
Expand Down Expand Up @@ -117,7 +116,7 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
input: parameter,
context: this.context,
accessPath: this.accessPath,
pathId: `parameters[${index}]`,
pathId: ["parameters", `${index}`],
});
}
// this.pathParameters.push(parameter.name);
Expand All @@ -129,7 +128,7 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
input: parameter,
context: this.context,
accessPath: this.accessPath,
pathId: `parameters[${index}]`,
pathId: ["parameters", `${index}`],
});
}
} else if (parameter.in === "header") {
Expand All @@ -140,7 +139,7 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
input: parameter,
context: this.context,
accessPath: this.accessPath,
pathId: `parameters[${index}]`,
pathId: ["parameters", `${index}`],
});
}
}
Expand Down Expand Up @@ -352,9 +351,7 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
path:
this.convertPathToPathParts()?.map((part) => part.value.toString()) ??
[],
headers: dedupPayloads(
convertOperationObjectProperties(this.requestHeaders)?.flat()
),
headers: convertOperationObjectProperties(this.requestHeaders)?.flat(),
payloads: this.requests?.convertToWebhookPayload(),
examples: [this.requests?.webhookExample()].filter(isNonNullish),
};
Expand All @@ -374,6 +371,10 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
authIds = Object.keys(auth);
}

const responseHeaders = responses
?.flatMap((response) => response.headers)
.filter(isNonNullish);

return {
id: FernRegistry.EndpointId(this.endpointId),
description: this.description,
Expand All @@ -386,18 +387,19 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
auth: authIds?.map((id) => FernRegistry.api.latest.AuthSchemeId(id)),
defaultEnvironment: environments?.[0]?.id,
environments,
pathParameters: dedupPayloads(
convertOperationObjectProperties(this.pathParameters)?.flat()
),
queryParameters: dedupPayloads(
convertOperationObjectProperties(this.queryParameters)?.flat()
),
requestHeaders: dedupPayloads(
convertOperationObjectProperties(this.requestHeaders)?.flat()
),
responseHeaders: dedupPayloads(
responses?.flatMap((response) => response.headers).filter(isNonNullish)
),
pathParameters: convertOperationObjectProperties(
this.pathParameters
)?.flat(),
queryParameters: convertOperationObjectProperties(
this.queryParameters
)?.flat(),
requestHeaders: convertOperationObjectProperties(
this.requestHeaders
)?.flat(),
responseHeaders:
responseHeaders != null && responseHeaders.length > 0
? responseHeaders
: undefined,
requests: this.requests?.convert(),
responses: responses?.map((response) => response.response),
errors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export class PathItemObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
this.context,
this.accessPath
);
const path = Array.isArray(this.pathId)
? this.pathId.join("/")
: this.pathId;

if (this.input.get != null) {
this.get = new OperationObjectConverterNode(
Expand All @@ -56,7 +59,7 @@ export class PathItemObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
},
this.servers,
this.globalAuth,
this.pathId,
path,
"GET",
this.basePath,
this.isWebhook
Expand All @@ -72,7 +75,7 @@ export class PathItemObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
},
this.servers,
this.globalAuth,
this.pathId,
path,
"POST",
this.basePath,
this.isWebhook
Expand All @@ -88,7 +91,7 @@ export class PathItemObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
},
this.servers,
this.globalAuth,
this.pathId,
path,
"PUT",
this.basePath
);
Expand All @@ -103,7 +106,7 @@ export class PathItemObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
},
this.servers,
this.globalAuth,
this.pathId,
path,
"PATCH",
this.basePath
);
Expand All @@ -118,7 +121,7 @@ export class PathItemObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
},
this.servers,
this.globalAuth,
this.pathId,
path,
"DELETE",
this.basePath
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class ResponseObjectConverterNode extends BaseOpenApiV3_1ConverterNode<
input: contentTypeObject,
context: this.context,
accessPath: this.accessPath,
pathId: "content",
pathId: ["content", contentType],
},
contentType,
streamingFormat,
Expand Down
11 changes: 7 additions & 4 deletions packages/parsers/src/openapi/BaseOpenApiV3_1Converter.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type BaseOpenApiV3_1ConverterNodeConstructorArgs<Input> = {
input: Input;
context: BaseOpenApiV3_1ConverterNodeContext;
readonly accessPath: string[];
readonly pathId: string;
readonly pathId: string | string[];
};

export abstract class BaseOpenApiV3_1ConverterNode<
Expand All @@ -22,7 +22,7 @@ export abstract class BaseOpenApiV3_1ConverterNode<
> extends BaseApiConverterNode<Input, Output> {
protected override readonly context: BaseOpenApiV3_1ConverterNodeContext;
protected readonly accessPath: string[];
protected readonly pathId: string;
protected readonly pathId: string | string[];

constructor({
input,
Expand All @@ -37,10 +37,13 @@ export abstract class BaseOpenApiV3_1ConverterNode<
this.pathId = pathId;

if (
this.pathId &&
this.pathId != null &&
this.pathId !== this.accessPath[this.accessPath.length - 1]
) {
this.accessPath.push(this.pathId);
this.accessPath.push(
...(Array.isArray(this.pathId) ? this.pathId : [this.pathId])
);

context.logger.debug(`Processing ${toOpenApiPath(this.accessPath)}`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { OpenApiDocumentConverterNode } from "../3.1/OpenApiDocumentConverter.no
import { BaseOpenApiV3_1ConverterNodeContext } from "../BaseOpenApiV3_1Converter.node";

function replaceEndpointUUIDs(json: string): string {
return json.replace(
return json.replaceAll(
/"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"/g,
'"test-uuid-replacement"'
);
Expand Down Expand Up @@ -50,20 +50,22 @@ describe("OpenAPI snapshot tests", () => {
input: parsed,
context,
accessPath: [],
pathId: directory,
pathId: undefined,
});
errors.push(...converter.errors());
warnings.push(...converter.warnings());
converted = converter.convert();
}

// Create snapshot
if (errors.length > 0) {
// console.error("errors:", errors);
await expect(errors).toMatchFileSnapshot(
`./__snapshots__/pathing/errors/${directory}.txt`
);
}
// expect(errors).toHaveLength(0);
if (warnings.length > 0) {
// console.warn("warnings:", warnings);
await expect(warnings).toMatchFileSnapshot(
`./__snapshots__/pathing/warnings/${directory}.txt`
);
}

converted.id = "test-uuid-replacement";
Expand Down
19 changes: 19 additions & 0 deletions packages/parsers/src/openapi/__test__/__snapshots__/cohere.json
Original file line number Diff line number Diff line change
Expand Up @@ -3431,6 +3431,25 @@
}
],
"responseHeaders": [
{
"key": "X-API-Warning",
"valueShape": {
"type": "alias",
"value": {
"type": "optional",
"shape": {
"type": "alias",
"value": {
"type": "primitive",
"value": {
"type": "string"
}
}
}
}
},
"description": "Warning description for incorrect usage of the API"
},
{
"key": "X-API-Warning",
"valueShape": {
Expand Down
Loading
Loading