Skip to content

Commit

Permalink
allow freeform aliases to be request body types
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Feb 5, 2025
1 parent 4a34d3f commit 242ddfa
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import { resolveReference } from "../../../utils/3.1/resolveReference";
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 +32,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 Down Expand Up @@ -102,13 +101,13 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
},
false
);
} 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 @@ -168,8 +167,23 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
| FernRegistry.api.latest.HttpRequestBodyShape[]
| undefined {
switch (this.contentType) {
case "json":
return this.schema?.convert();
case "json": {
const convertedJsonSchema = this.schema?.convert();
if (convertedJsonSchema == null) {
return undefined;
}
const convertedJsonSchemaArray =
maybeSingleValueToArray(convertedJsonSchema);

return convertedJsonSchemaArray
?.map((convertedJsonSchema) =>
convertedJsonSchema.type === "object" ||
convertedJsonSchema.type === "alias"
? convertedJsonSchema
: undefined
)
.filter(isNonNullish);
}
case "bytes":
return {
type: "bytes",
Expand Down Expand Up @@ -243,8 +257,23 @@ export class RequestMediaTypeObjectConverterNode extends BaseOpenApiV3_1Converte
description: this.description,
}));
}
case undefined:
return this.schema?.convert();
case undefined: {
const convertedJsonSchema = this.schema?.convert();
if (convertedJsonSchema == null) {
return undefined;
}
const convertedJsonSchemaArray =
maybeSingleValueToArray(convertedJsonSchema);

return convertedJsonSchemaArray
?.map((convertedJsonSchema) =>
convertedJsonSchema.type === "object" ||
convertedJsonSchema.type === "alias"
? convertedJsonSchema
: undefined
)
.filter(isNonNullish);
}
default:
return undefined;
}
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": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15757,16 +15757,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -17208,16 +17199,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -21963,16 +21945,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -33065,16 +33038,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -35746,9 +35710,21 @@
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
"type": "alias",
"value": {
"type": "id",
"id": "contact_reply_ticket_request"
}
}
},
{
"contentType": "application/json",
"body": {
"type": "alias",
"value": {
"type": "id",
"id": "admin_reply_ticket_request"
}
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15757,16 +15757,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -17208,16 +17199,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -21963,16 +21945,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -33065,16 +33038,7 @@
}
}
],
"requests": [
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
}
}
],
"requests": [],
"responses": [
{
"statusCode": 200,
Expand Down Expand Up @@ -35746,9 +35710,21 @@
{
"contentType": "application/json",
"body": {
"type": "object",
"extends": [],
"properties": []
"type": "alias",
"value": {
"type": "id",
"id": "contact_reply_ticket_request"
}
}
},
{
"contentType": "application/json",
"body": {
"type": "alias",
"value": {
"type": "id",
"id": "admin_reply_ticket_request"
}
}
}
],
Expand Down
Loading

0 comments on commit 242ddfa

Please sign in to comment.