Skip to content

Commit

Permalink
fix: refer Schema namespace in generated body type
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPetricola authored and astahmer committed Nov 22, 2023
1 parent 60bc8fc commit bb937d4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-coins-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typed-openapi": patch
---

fix: refer Schema namespace in generated body type
19 changes: 16 additions & 3 deletions packages/typed-openapi/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { capitalize, groupBy } from "pastable/server";
import { Box } from "./box";
import { prettify } from "./format";
import { mapOpenApiEndpoints } from "./map-openapi-endpoints";
import { AnyBox, BoxRef } from "./types";
import { AnyBox, AnyBoxDef, BoxRef } from "./types";
import * as Codegen from "@sinclair/typebox-codegen";
import { match } from "ts-pattern";
import { type } from "arktype";
Expand Down Expand Up @@ -117,7 +117,7 @@ const generateSchemaList = ({ refs, runtime }: GeneratorContext) => {
);
};

const parameterObjectToString = (parameters: Box<BoxRef> | Record<string, AnyBox>) => {
const parameterObjectToString = (parameters: Box<AnyBoxDef> | Record<string, AnyBox>) => {
if (parameters instanceof Box) return parameters.value;

let str = "{";
Expand All @@ -143,7 +143,20 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
${parameters.query ? `query: ${parameterObjectToString(parameters.query)},` : ""}
${parameters.path ? `path: ${parameterObjectToString(parameters.path)},` : ""}
${parameters.header ? `header: ${parameterObjectToString(parameters.header)},` : ""}
${parameters.body ? `body: ${parameterObjectToString(parameters.body)},` : ""}
${
parameters.body
? `body: ${parameterObjectToString(
ctx.runtime === "none"
? parameters.body.recompute((box) => {
if (Box.isReference(box) && !box.params.generics) {
box.value = `Schemas.${box.value}`;
}
return box;
})
: parameters.body,
)},`
: ""
}
}`
: "parameters: never,"
}
Expand Down
12 changes: 6 additions & 6 deletions packages/typed-openapi/tests/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ describe("generator", () => {
method: "PUT";
path: "/pet";
parameters: {
body: Pet;
body: Schemas.Pet;
};
response: Schemas.Pet;
};
export type post_AddPet = {
method: "POST";
path: "/pet";
parameters: {
body: Pet;
body: Schemas.Pet;
};
response: Schemas.Pet;
};
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("generator", () => {
method: "POST";
path: "/store/order";
parameters: {
body: Order;
body: Schemas.Order;
};
response: Schemas.Order;
};
Expand All @@ -151,15 +151,15 @@ describe("generator", () => {
method: "POST";
path: "/user";
parameters: {
body: User;
body: Schemas.User;
};
response: Schemas.User;
};
export type post_CreateUsersWithListInput = {
method: "POST";
path: "/user/createWithList";
parameters: {
body: Array<User>;
body: Array<Schemas.User>;
};
response: Schemas.User;
};
Expand Down Expand Up @@ -191,7 +191,7 @@ describe("generator", () => {
parameters: {
path: { username: string };
body: User;
body: Schemas.User;
};
response: unknown;
};
Expand Down
43 changes: 22 additions & 21 deletions packages/typed-openapi/tests/snapshots/docker.openapi.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,8 @@ export namespace Endpoints {
parameters: {
query: Partial<{ name: string; platform: string }>;

body: ContainerConfig & Partial<{ HostConfig: HostConfig; NetworkingConfig: NetworkingConfig }>;
body: Schemas.ContainerConfig &
Partial<{ HostConfig: Schemas.HostConfig; NetworkingConfig: Schemas.NetworkingConfig }>;
};
response: Schemas.ContainerCreateResponse;
};
Expand Down Expand Up @@ -1005,7 +1006,7 @@ export namespace Endpoints {
parameters: {
path: { id: string };

body: Resources & Partial<{ RestartPolicy: RestartPolicy }>;
body: Schemas.Resources & Partial<{ RestartPolicy: Schemas.RestartPolicy }>;
};
response: Partial<{ Warnings: Array<string> }>;
};
Expand Down Expand Up @@ -1265,7 +1266,7 @@ export namespace Endpoints {
method: "POST";
path: "/auth";
parameters: {
body: AuthConfig;
body: Schemas.AuthConfig;
};
response: unknown;
};
Expand Down Expand Up @@ -1307,7 +1308,7 @@ export namespace Endpoints {
changes: string;
}>;

body: ContainerConfig;
body: Schemas.ContainerConfig;
};
response: Schemas.IdResponse;
};
Expand Down Expand Up @@ -1367,7 +1368,7 @@ export namespace Endpoints {
AttachStdin: boolean;
AttachStdout: boolean;
AttachStderr: boolean;
ConsoleSize: Array<number> | null;
ConsoleSize: Array<number> | Schemas.null;
DetachKeys: string;
Tty: boolean;
Env: Array<string>;
Expand All @@ -1385,7 +1386,7 @@ export namespace Endpoints {
parameters: {
path: { id: string };

body: Partial<{ Detach: boolean; Tty: boolean; ConsoleSize: Array<number> | null }>;
body: Partial<{ Detach: boolean; Tty: boolean; ConsoleSize: Array<number> | Schemas.null }>;
};
response: unknown;
};
Expand Down Expand Up @@ -1430,7 +1431,7 @@ export namespace Endpoints {
method: "POST";
path: "/volumes/create";
parameters: {
body: VolumeCreateOptions;
body: Schemas.VolumeCreateOptions;
};
response: Schemas.Volume;
};
Expand All @@ -1449,7 +1450,7 @@ export namespace Endpoints {
query: { version: number };
path: { name: string };

body: Partial<{ Spec: ClusterVolumeSpec }>;
body: Partial<{ Spec: Schemas.ClusterVolumeSpec }>;
};
response: unknown;
};
Expand Down Expand Up @@ -1506,7 +1507,7 @@ export namespace Endpoints {
Internal?: boolean | undefined;
Attachable?: boolean | undefined;
Ingress?: boolean | undefined;
IPAM?: IPAM | undefined;
IPAM?: Schemas.IPAM | undefined;
EnableIPv6?: boolean | undefined;
Options?: unknown | undefined;
Labels?: unknown | undefined;
Expand All @@ -1520,7 +1521,7 @@ export namespace Endpoints {
parameters: {
path: { id: string };

body: Partial<{ Container: string; EndpointConfig: EndpointSettings }>;
body: Partial<{ Container: string; EndpointConfig: Schemas.EndpointSettings }>;
};
response: unknown;
};
Expand Down Expand Up @@ -1565,7 +1566,7 @@ export namespace Endpoints {
query: { remote: string; name: string };

header: Partial<{ "X-Registry-Auth": string }>;
body: Array<PluginPrivilege>;
body: Array<Schemas.PluginPrivilege>;
};
response: unknown;
};
Expand Down Expand Up @@ -1611,7 +1612,7 @@ export namespace Endpoints {
query: { remote: string };
path: { name: string };
header: Partial<{ "X-Registry-Auth": string }>;
body: Array<PluginPrivilege>;
body: Array<Schemas.PluginPrivilege>;
};
response: unknown;
};
Expand Down Expand Up @@ -1673,7 +1674,7 @@ export namespace Endpoints {
query: { version: number };
path: { id: string };

body: NodeSpec;
body: Schemas.NodeSpec;
};
response: unknown;
};
Expand All @@ -1695,7 +1696,7 @@ export namespace Endpoints {
DefaultAddrPool: Array<string>;
ForceNewCluster: boolean;
SubnetSize: number;
Spec: SwarmSpec;
Spec: Schemas.SwarmSpec;
}>;
};
response: string;
Expand Down Expand Up @@ -1733,7 +1734,7 @@ export namespace Endpoints {
rotateManagerUnlockKey: boolean;
};

body: SwarmSpec;
body: Schemas.SwarmSpec;
};
response: unknown;
};
Expand Down Expand Up @@ -1764,7 +1765,7 @@ export namespace Endpoints {
path: "/services/create";
parameters: {
header: Partial<{ "X-Registry-Auth": string }>;
body: ServiceSpec & unknown;
body: Schemas.ServiceSpec & unknown;
};
response: Partial<{ ID: string; Warning: string }>;
};
Expand Down Expand Up @@ -1792,7 +1793,7 @@ export namespace Endpoints {
query: { version: number; registryAuthFrom: "spec" | "previous-spec"; rollback: string };
path: { id: string };
header: Partial<{ "X-Registry-Auth": string }>;
body: ServiceSpec & unknown;
body: Schemas.ServiceSpec & unknown;
};
response: Schemas.ServiceUpdateResponse;
};
Expand Down Expand Up @@ -1858,7 +1859,7 @@ export namespace Endpoints {
method: "POST";
path: "/secrets/create";
parameters: {
body: SecretSpec & unknown;
body: Schemas.SecretSpec & unknown;
};
response: Schemas.IdResponse;
};
Expand All @@ -1885,7 +1886,7 @@ export namespace Endpoints {
query: { version: number };
path: { id: string };

body: SecretSpec;
body: Schemas.SecretSpec;
};
response: unknown;
};
Expand All @@ -1901,7 +1902,7 @@ export namespace Endpoints {
method: "POST";
path: "/configs/create";
parameters: {
body: ConfigSpec & unknown;
body: Schemas.ConfigSpec & unknown;
};
response: Schemas.IdResponse;
};
Expand All @@ -1928,7 +1929,7 @@ export namespace Endpoints {
query: { version: number };
path: { id: string };

body: ConfigSpec;
body: Schemas.ConfigSpec;
};
response: unknown;
};
Expand Down
12 changes: 6 additions & 6 deletions packages/typed-openapi/tests/snapshots/petstore.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export namespace Endpoints {
method: "PUT";
path: "/pet";
parameters: {
body: Pet;
body: Schemas.Pet;
};
response: Schemas.Pet;
};
export type post_AddPet = {
method: "POST";
path: "/pet";
parameters: {
body: Pet;
body: Schemas.Pet;
};
response: Schemas.Pet;
};
Expand Down Expand Up @@ -117,7 +117,7 @@ export namespace Endpoints {
method: "POST";
path: "/store/order";
parameters: {
body: Order;
body: Schemas.Order;
};
response: Schemas.Order;
};
Expand All @@ -141,15 +141,15 @@ export namespace Endpoints {
method: "POST";
path: "/user";
parameters: {
body: User;
body: Schemas.User;
};
response: Schemas.User;
};
export type post_CreateUsersWithListInput = {
method: "POST";
path: "/user/createWithList";
parameters: {
body: Array<User>;
body: Array<Schemas.User>;
};
response: Schemas.User;
};
Expand Down Expand Up @@ -181,7 +181,7 @@ export namespace Endpoints {
parameters: {
path: { username: string };

body: User;
body: Schemas.User;
};
response: unknown;
};
Expand Down

0 comments on commit bb937d4

Please sign in to comment.