Skip to content

Commit

Permalink
fix(cli): Correctly use parsed parameter location in generated fern d…
Browse files Browse the repository at this point in the history
…efinition.
  • Loading branch information
eyw520 committed Feb 18, 2025
1 parent 9e644fa commit aaca0aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { FernAsyncAPIExtension } from "../fernExtensions";
import { WebsocketSessionExampleExtension, getFernExamples } from "../getFernExamples";
import { ParseAsyncAPIOptions } from "../options";
import { AsyncAPIIntermediateRepresentation } from "../parse";
import { transformToValidPath } from "../sharedUtils";
import { AsyncAPIV2 } from "../v2";
import { AsyncAPIV2ParserContext } from "./AsyncAPIV2ParserContext";
import { ExampleWebsocketSessionFactory } from "./ExampleWebsocketSessionFactory";
Expand Down Expand Up @@ -250,7 +249,7 @@ export function parseAsyncAPIV2({
subscribe:
subscribeSchema != null ? convertSchemaWithExampleToSchema(subscribeSchema) : subscribeSchema,
summary: getExtension<string | undefined>(channel, FernAsyncAPIExtension.FERN_DISPLAY_NAME),
path: transformToValidPath(channelPath),
path: channelPath,
description: undefined,
examples,
source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { getSchemas } from "../../utils/getSchemas";
import { FernAsyncAPIExtension } from "../fernExtensions";
import { ParseAsyncAPIOptions } from "../options";
import { AsyncAPIIntermediateRepresentation } from "../parse";
import { transformToValidPath } from "../sharedUtils";
import { AsyncAPIV3 } from "../v3";
import { AsyncAPIV3ParserContext } from "./AsyncAPIV3ParserContext";

Expand Down Expand Up @@ -141,7 +140,7 @@ export function parseAsyncAPIV3({
const queryParameters: QueryParameterWithExample[] = [];
if (channel.parameters != null) {
for (const [name, parameter] of Object.entries(channel.parameters)) {
const { type, path } = convertChannelParameterLocation(parameter.location);
const { type, parameterKey } = convertChannelParameterLocation(parameter.location);
const parameterName = upperFirst(camelCase(channelPath)) + upperFirst(camelCase(name));
const parameterSchema =
parameter.enum != null && Array.isArray(parameter.enum)
Expand All @@ -165,13 +164,13 @@ export function parseAsyncAPIV3({
description: undefined,
availability: undefined,
generatedName: "",
title: undefined,
title: parameterName,
groupName: undefined,
nameOverride: undefined
});

const parameterObject = {
name,
name: parameterKey,
description: parameter.description,
parameterNameOverride: undefined,
schema: parameterSchema,
Expand Down Expand Up @@ -227,7 +226,9 @@ export function parseAsyncAPIV3({
? convertSchemaWithExampleToSchema(channelSchemas[channelPath].subscribe)
: undefined,
summary: getExtension<string | undefined>(channel, FernAsyncAPIExtension.FERN_DISPLAY_NAME),
path: transformToValidPath(channelPath),
// TODO (Eden): This can be a LOT more complicated than this. See the link below for more details:
// https://www.asyncapi.com/docs/reference/specification/v3.0.0#channelObject
path: channel.address ?? transformToValidPath(channelPath),
description: undefined,
examples: [],
source
Expand All @@ -249,9 +250,12 @@ function getChannelPathFromOperation(operation: AsyncAPIV3.Operation): string {
return operation.channel.$ref.substring(CHANNEL_REFERENCE_PREFIX.length);
}

function convertChannelParameterLocation(location: string): { type: "header" | "path" | "payload"; path: string } {
const [messageType, path] = location.split("#/");
if (messageType == null || path == null) {
function convertChannelParameterLocation(location: string): {
type: "header" | "path" | "payload";
parameterKey: string;
} {
const [messageType, parameterKey] = location.split("#/");
if (messageType == null || parameterKey == null) {
throw new Error(`Invalid location format: ${location}`);
}
if (!messageType.startsWith(LOCATION_PREFIX)) {
Expand All @@ -261,7 +265,14 @@ function convertChannelParameterLocation(location: string): { type: "header" | "
if (type !== "header" && type !== "path" && type !== "payload") {
throw new Error(`Invalid message type: ${type}. Must be one of: header, path, payload`);
}
return { type, path };
return { type, parameterKey };
}

function transformToValidPath(path: string): string {
if (!path.startsWith("/")) {
return "/" + path;
}
return path;
}

function buildEnumSchema({
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
- changelogEntry:
- summary: |
Correctly parse out channel address for v3 AsyncAPI specs.
type: fix
irVersion: 55
version: 0.53.6

- changelogEntry:
- summary: |
Support validation schemas in detailed Union types.
Expand Down

0 comments on commit aaca0aa

Please sign in to comment.