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: OpenApi Parser v2 allow freeform aliases to be request body types #2123

Merged
merged 4 commits into from
Feb 6, 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
2 changes: 1 addition & 1 deletion packages/parsers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fern-api/docs-parsers",
"version": "0.0.51",
"version": "0.0.52",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern-platform.git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ export class OperationObjectConverterNode extends BaseOpenApiV3_1ConverterNode<

this.requests =
this.input.requestBody != null
? new RequestBodyObjectConverterNode({
input: this.input.requestBody,
context: this.context,
accessPath: this.accessPath,
pathId: "requestBody",
})
? new RequestBodyObjectConverterNode(
{
input: this.input.requestBody,
context: this.context,
accessPath: this.accessPath,
pathId: "requestBody",
},
this.method,
this.path
)
: undefined;

this.responses =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
BaseOpenApiV3_1ConverterNode,
BaseOpenApiV3_1ConverterNodeConstructorArgs,
} from "../../../BaseOpenApiV3_1Converter.node";
import { HttpMethod } from "../../../constants";
import { resolveRequestReference } from "../../../utils/3.1/resolveRequestReference";
import { maybeSingleValueToArray } from "../../../utils/maybeSingleValueToArray";
import { RequestMediaTypeObjectConverterNode } from "./RequestMediaTypeObjectConverter.node";
Expand All @@ -21,7 +22,9 @@ export class RequestBodyObjectConverterNode extends BaseOpenApiV3_1ConverterNode
constructor(
args: BaseOpenApiV3_1ConverterNodeConstructorArgs<
OpenAPIV3_1.RequestBodyObject | OpenAPIV3_1.ReferenceObject
>
>,
protected method: HttpMethod,
protected path: string
) {
super(args);
this.safeParse();
Expand Down Expand Up @@ -52,7 +55,9 @@ export class RequestBodyObjectConverterNode extends BaseOpenApiV3_1ConverterNode
accessPath: this.accessPath,
pathId: "content",
},
contentType
contentType,
this.method,
this.path
);
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { isNonNullish } from "@fern-api/ui-core-utils";
import { camelCase } from "es-toolkit";
import { OpenAPIV3_1 } from "openapi-types";
import { UnreachableCaseError } from "ts-essentials";
import { FernRegistry } from "../../../../client/generated";
import { HttpMethod } from "../../../../client/generated/api";
import {
BaseOpenApiV3_1ConverterNode,
BaseOpenApiV3_1ConverterNodeConstructorArgs,
Expand All @@ -11,12 +13,12 @@ import {
SUPPORTED_REQUEST_CONTENT_TYPES,
} from "../../../types/format.types";
import { resolveReference } from "../../../utils/3.1/resolveReference";
import { createTypeDefinition } from "../../../utils/createTypeDefinition";
import { maybeSingleValueToArray } from "../../../utils/maybeSingleValueToArray";
import { MediaType } from "../../../utils/MediaType";
import { AvailabilityConverterNode } from "../../extensions/AvailabilityConverter.node";
import { isObjectSchema } from "../../guards/isObjectSchema";
import { isReferenceObject } from "../../guards/isReferenceObject";
import { ObjectConverterNode } from "../../schemas/ObjectConverter.node";
import { SchemaConverterNode } from "../../schemas";
import { ReferenceConverterNode } from "../../schemas/ReferenceConverter.node";
import { GLOBAL_EXAMPLE_NAME } from "../ExampleObjectConverter.node";
import { MultipartFormDataPropertySchemaConverterNode } from "./MultipartFormDataPropertySchemaConverter.node";
Expand All @@ -33,7 +35,7 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
description: string | undefined;

// application/json
schema: ReferenceConverterNode | ObjectConverterNode | undefined;
schema: ReferenceConverterNode | SchemaConverterNode | undefined;

// application/octet-stream
isOptional: boolean | undefined;
Expand All @@ -53,7 +55,9 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte

constructor(
args: BaseOpenApiV3_1ConverterNodeConstructorArgs<OpenAPIV3_1.MediaTypeObject>,
contentType: string | undefined
contentType: string | undefined,
protected method: HttpMethod,
protected path: string
) {
super(args);
this.safeParse(contentType);
Expand Down Expand Up @@ -100,15 +104,22 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
pathId: "schema",
seenSchemas: new Set(),
},
false
false,
this.input.schema.description,
new AvailabilityConverterNode({
input: this.input.schema,
context: this.context,
accessPath: this.accessPath,
pathId: "availability",
})
);
} else if (isObjectSchema(this.input.schema)) {
} else {
this.resolvedSchema = this.input.schema;
const mediaType = MediaType.parse(contentType);
// An exhaustive switch cannot be used here, because contentType is an unbounded string
if (mediaType?.containsJSON()) {
this.contentType = "json" as const;
this.schema = new ObjectConverterNode({
this.schema = new SchemaConverterNode({
input: this.input.schema,
context: this.context,
accessPath: this.accessPath,
Expand Down Expand Up @@ -163,13 +174,71 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
}
}

convertJsonLike():
| FernRegistry.api.latest.HttpRequestBodyShape
| FernRegistry.api.latest.HttpRequestBodyShape[]
| undefined {
const convertedJsonSchema = this.schema?.convert();
if (convertedJsonSchema == null) {
return undefined;
}
const convertedJsonSchemaArray =
maybeSingleValueToArray(convertedJsonSchema);

return convertedJsonSchemaArray
?.map((convertedJsonSchema) => {
const type = convertedJsonSchema.type;
switch (type) {
case "object":
case "alias":
return convertedJsonSchema;
case "enum":
case "undiscriminatedUnion":
case "discriminatedUnion": {
const uniqueId = camelCase(
[this.method, this.path, this.contentType, "request"].join("_")
);
createTypeDefinition({
uniqueId,
type: convertedJsonSchema,
contextTypes: this.context.generatedTypes,
description: this.schema?.description,
availability: this.schema?.availability?.convert(),
});
return {
type: "alias" as const,
value: {
type: "id" as const,
id: FernRegistry.TypeId(uniqueId),
default:
convertedJsonSchema.type === "enum" &&
convertedJsonSchema.default != null
? {
type: "enum" as const,
value: convertedJsonSchema.default,
}
: undefined,
},
};
}
case undefined:
return undefined;
default:
new UnreachableCaseError(type);
return undefined;
}
})
.filter(isNonNullish);
}

convert():
| FernRegistry.api.latest.HttpRequestBodyShape
| FernRegistry.api.latest.HttpRequestBodyShape[]
| undefined {
switch (this.contentType) {
case "json":
return this.schema?.convert();
case "json": {
return this.convertJsonLike();
}
case "bytes":
return {
type: "bytes",
Expand Down Expand Up @@ -244,8 +313,9 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
description: this.description,
}));
}
case undefined:
return this.schema?.convert();
case undefined: {
return this.convertJsonLike();
}
default:
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,6 @@ export class ResponseMediaTypeObjectConverterNode extends BaseOpenApiV3_1Convert
case "application/json":
if (this.streamingFormat == null) {
return this.convertJsonLike();
// const maybeShapes = maybeSingleValueToArray(this.schema?.convert());

// return maybeShapes
// ?.map((shape) => {
// if (
// shape == null ||
// (shape.type !== "object" && shape.type !== "alias")
// ) {
// return undefined;
// }
// return shape;
// })
// .filter(isNonNullish);
} else {
return this.convertStreamingFormat();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "../../BaseOpenApiV3_1Converter.node";
import { getSchemaIdFromReference } from "../../utils/3.1/getSchemaIdFromReference";
import { resolveSchemaReference } from "../../utils/3.1/resolveSchemaReference";
import { AvailabilityConverterNode } from "../extensions/AvailabilityConverter.node";
import { isNonArraySchema } from "../guards/isNonArraySchema";
import { SchemaConverterNode } from "./SchemaConverter.node";
import { EnumConverterNode } from "./primitives/EnumConverter.node";
Expand All @@ -20,7 +21,9 @@ export class ReferenceConverterNode extends BaseOpenApiV3_1ConverterNodeWithTrac

constructor(
args: BaseOpenApiV3_1ConverterNodeWithTrackingConstructorArgs<OpenAPIV3_1.ReferenceObject>,
protected nullable: boolean | undefined
protected nullable: boolean | undefined,
public description: string | undefined,
public availability: AvailabilityConverterNode | undefined
) {
super(args);
this.safeParse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export class SchemaConverterNode extends BaseOpenApiV3_1ConverterNodeWithTrackin
pathId: refPath,
seenSchemas: this.seenSchemas,
},
this.nullable
this.nullable,
this.description,
this.availability
);
} else {
// If the object is not a reference object, then it is a schema object, gather all appropriate variables
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"id": "test-uuid-replacement",
"endpoints": {
"endpoint_.postTestNestedArrayObject": {
"id": "endpoint_.postTestNestedArrayObject",
"method": "POST",
"path": [
{
"type": "literal",
"value": "/"
},
{
"type": "literal",
"value": "test"
},
{
"type": "literal",
"value": "/"
},
{
"type": "literal",
"value": "nested"
},
{
"type": "literal",
"value": "/"
},
{
"type": "literal",
"value": "array-object"
}
],
"environments": [],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "alias",
"value": {
"type": "list",
"itemShape": {
"type": "object",
"extends": [],
"properties": [
{
"key": "a",
"valueShape": {
"type": "alias",
"value": {
"type": "primitive",
"value": {
"type": "string"
}
}
}
},
{
"key": "b",
"valueShape": {
"type": "alias",
"value": {
"type": "primitive",
"value": {
"type": "string"
}
}
}
}
]
}
}
}
}
],
"protocol": {
"type": "rest"
}
}
},
"websockets": {},
"webhooks": {},
"types": {},
"subpackages": {},
"auths": {}
}
Loading
Loading