Skip to content

Commit

Permalink
fix(cli): support parsing and generating webhook examples (#5063)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Nov 1, 2024
1 parent 00dd9f4 commit ad4f9d4
Show file tree
Hide file tree
Showing 13 changed files with 10,532 additions and 5,707 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { EndpointSdkName, EndpointWithExample, HttpMethod, SdkGroupName, Webhook } from "@fern-api/openapi-ir";
import {
EndpointSdkName,
EndpointWithExample,
HttpMethod,
SdkGroupName,
WebhookWithExample
} from "@fern-api/openapi-ir";
import { camelCase } from "lodash-es";
import { OpenAPIV3 } from "openapi-types";
import { getExtension } from "../../../../getExtension";
Expand Down Expand Up @@ -32,7 +38,7 @@ export interface ConvertedHttpOperation {

export interface ConvertedWebhookOperation {
type: "webhook";
value: Webhook;
value: WebhookWithExample;
}

export interface ConvertedStreamingOperation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NamedFullExample, Source, Webhook, WebhookExampleCall } from "@fern-api/openapi-ir";
import { NamedFullExample, Source, Webhook, WebhookExampleCall, WebhookWithExample } from "@fern-api/openapi-ir";
import { convertToFullExample } from "../../../../schema/examples/convertToFullExample";
import { getGeneratedTypeName } from "../../../../schema/utils/getSchemaName";
import { AbstractOpenAPIV3ParserContext } from "../../AbstractOpenAPIV3ParserContext";
Expand All @@ -14,7 +14,7 @@ export function convertWebhookOperation({
operationContext: OperationContext;
context: AbstractOpenAPIV3ParserContext;
source: Source;
}): Webhook | undefined {
}): WebhookWithExample | undefined {
const { document, operation, path, method, baseBreadcrumbs, sdkMethodName } = operationContext;
const payloadBreadcrumbs = [...baseBreadcrumbs, "Payload"];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
SchemaWithExample,
SecurityScheme,
Source,
Webhook
Webhook,
WebhookExampleCall,
WebhookWithExample
} from "@fern-api/openapi-ir";
import { TaskContext } from "@fern-api/task-context";
import { mapValues } from "lodash-es";
Expand Down Expand Up @@ -96,7 +98,7 @@ export function generateIr({
const audiences = options.audiences ?? [];

const endpointsWithExample: EndpointWithExample[] = [];
const webhooks: Webhook[] = [];
const webhooksWithExample: WebhookWithExample[] = [];
Object.entries(openApi.paths ?? {}).forEach(([path, pathItem]) => {
if (pathItem == null) {
return;
Expand Down Expand Up @@ -124,7 +126,7 @@ export function generateIr({
}
break;
case "webhook":
webhooks.push(operation.value);
webhooksWithExample.push(operation.value);
break;
default:
assertNever(operation);
Expand All @@ -142,7 +144,7 @@ export function generateIr({
if (audiences.length > 0 && !audiences.some((audience) => webhookAudiences.includes(audience))) {
continue;
}
webhooks.push(webhook.value);
webhooksWithExample.push(webhook.value);
}
});

Expand Down Expand Up @@ -189,6 +191,39 @@ export function generateIr({
}

const exampleTypeFactory = new ExampleTypeFactory(schemasWithDiscriminants);

const webhooks: Webhook[] = webhooksWithExample.map((webhookWithExample) => {
const extensionExamples = webhookWithExample.examples;
let examples: WebhookExampleCall[] = extensionExamples;
if (!options.disableExamples && examples.length === 0) {
const webhookExample = exampleTypeFactory.buildExample({
schema: webhookWithExample.payload,
exampleId: undefined,
example: undefined,
skipReadonly: false,
options: {
ignoreOptionals: false,
isParameter: false
}
});
if (webhookExample != null) {
examples = [
{
name: undefined,
description: undefined,
payload: webhookExample
}
];
}
}

return {
...webhookWithExample,
payload: convertSchemaWithExampleToSchema(webhookWithExample.payload),
examples
};
});

const exampleEndpointFactory = new ExampleEndpointFactory(schemasWithDiscriminants, context.logger);
const endpoints = endpointsWithExample.map((endpointWithExample): Endpoint => {
// if x-fern-examples is not present, generate an example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { OpenAPIV3 } from "openapi-types";
import { getExtension } from "../getExtension";
import { OpenAPIExtension } from "../openapi/v3/extensions/extensions";
import { FernOpenAPIExtension } from "../openapi/v3/extensions/fernExtensions";
import { getExamples } from "../openapi/v3/extensions/getExamples";
import { getFernEncoding } from "../openapi/v3/extensions/getFernEncoding";
import { getFernEnum } from "../openapi/v3/extensions/getFernEnum";
import { getFernTypeExtension } from "../openapi/v3/extensions/getFernTypeExtension";
Expand Down Expand Up @@ -184,7 +185,7 @@ export function convertSchemaObject(
);
}

const examples = getExtension<unknown[]>(schema, "examples");
const examples = getExamples(schema);
if (examples != null && Object.keys(examples).length > 0) {
fullExamples.push(
...examples.map((value) => {
Expand Down
Loading

0 comments on commit ad4f9d4

Please sign in to comment.