Skip to content

Commit

Permalink
handle array response even though we shouldn't have them (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-crespo authored Apr 5, 2024
1 parent f036844 commit 4217d12
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
8 changes: 4 additions & 4 deletions client/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6221,7 +6221,7 @@ export class Api extends HttpClient {
* Get BFD status
*/
networkingBfdStatus: (_: EmptyObj, params: FetchParams = {}) => {
return this.request<void>({
return this.request<BfdStatus[]>({
path: `/v1/system/networking/bfd-status`,
method: "GET",
...params,
Expand Down Expand Up @@ -6276,7 +6276,7 @@ export class Api extends HttpClient {
{ query }: { query?: NetworkingBgpAnnounceSetListQueryParams },
params: FetchParams = {}
) => {
return this.request<void>({
return this.request<BgpAnnouncement[]>({
path: `/v1/system/networking/bgp-announce`,
method: "GET",
query,
Expand Down Expand Up @@ -6318,7 +6318,7 @@ export class Api extends HttpClient {
{ query }: { query?: NetworkingBgpImportedRoutesIpv4QueryParams },
params: FetchParams = {}
) => {
return this.request<void>({
return this.request<BgpImportedRouteIpv4[]>({
path: `/v1/system/networking/bgp-routes-ipv4`,
method: "GET",
query,
Expand All @@ -6329,7 +6329,7 @@ export class Api extends HttpClient {
* Get BGP peer status
*/
networkingBgpStatus: (_: EmptyObj, params: FetchParams = {}) => {
return this.request<void>({
return this.request<BgpPeerStatus[]>({
path: `/v1/system/networking/bgp-status`,
method: "GET",
...params,
Expand Down
8 changes: 4 additions & 4 deletions client/msw-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ export interface MSWHandlers {
networkingBfdStatus: (params: {
req: Request;
cookies: Record<string, string>;
}) => Promisable<StatusCode>;
}) => Promisable<HandlerResult<Api.BfdStatus[]>>;
/** `GET /v1/system/networking/bgp` */
networkingBgpConfigList: (params: {
query: Api.NetworkingBgpConfigListQueryParams;
Expand All @@ -909,7 +909,7 @@ export interface MSWHandlers {
query: Api.NetworkingBgpAnnounceSetListQueryParams;
req: Request;
cookies: Record<string, string>;
}) => Promisable<StatusCode>;
}) => Promisable<HandlerResult<Api.BgpAnnouncement[]>>;
/** `POST /v1/system/networking/bgp-announce` */
networkingBgpAnnounceSetCreate: (params: {
body: Json<Api.BgpAnnounceSetCreate>;
Expand All @@ -927,12 +927,12 @@ export interface MSWHandlers {
query: Api.NetworkingBgpImportedRoutesIpv4QueryParams;
req: Request;
cookies: Record<string, string>;
}) => Promisable<StatusCode>;
}) => Promisable<HandlerResult<Api.BgpImportedRouteIpv4[]>>;
/** `GET /v1/system/networking/bgp-status` */
networkingBgpStatus: (params: {
req: Request;
cookies: Record<string, string>;
}) => Promisable<StatusCode>;
}) => Promisable<HandlerResult<Api.BgpPeerStatus[]>>;
/** `GET /v1/system/networking/loopback-address` */
networkingLoopbackAddressList: (params: {
query: Api.NetworkingLoopbackAddressListQueryParams;
Expand Down
11 changes: 3 additions & 8 deletions generator/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from "../util";
import { initIO } from "../io";
import type { Schema } from "../schema/base";
import { refToSchemaName } from "../schema/base";
import {
contentRef,
docComment,
Expand Down Expand Up @@ -226,19 +225,15 @@ export function generateApi(spec: OpenAPIV3.Document, destDir: string) {
(p) => "in" in p && p.in === "query"
) as OpenAPIV3.ParameterObject[];

const bodyTypeRef = contentRef(conf.requestBody);
const bodyType = bodyTypeRef ? refToSchemaName(bodyTypeRef) : null;
const bodyType = contentRef(conf.requestBody);

const successResponse =
conf.responses["200"] ||
conf.responses["201"] ||
conf.responses["202"] ||
conf.responses["204"];

const successTypeRef = contentRef(successResponse);
const successType = successTypeRef
? refToSchemaName(successTypeRef)
: "void";
const successType = contentRef(successResponse);

docComment(conf.summary || conf.description, schemaNames, io);

Expand Down Expand Up @@ -270,7 +265,7 @@ export function generateApi(spec: OpenAPIV3.Document, destDir: string) {
}

w(`params: FetchParams = {}) => {
return this.request<${successType}>({
return this.request<${successType || "void"}>({
path: ${pathToTemplateStr(path)},
method: "${method.toUpperCase()}",`);
if (bodyType) {
Expand Down
28 changes: 20 additions & 8 deletions generator/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import type { OpenAPIV3 } from "openapi-types";
import { topologicalSort } from "../util";
import type { IO } from "../io";
import type { Schema } from "../schema/base";
import { refToSchemaName, type Schema } from "../schema/base";
import { OpenAPIV3 as O } from "openapi-types";
const HttpMethods = O.HttpMethods;

Expand All @@ -36,14 +36,26 @@ export const jsdocLinkify = (s: string, schemaNames: string[]) =>
);

export function contentRef(
o: Schema | OpenAPIV3.RequestBodyObject | undefined
o: Schema | OpenAPIV3.RequestBodyObject | undefined,
prefix = ""
) {
return o &&
"content" in o &&
o.content?.["application/json"]?.schema &&
"$ref" in o.content["application/json"].schema
? o.content["application/json"].schema.$ref
: null;
if (!(o && "content" in o && o.content?.["application/json"]?.schema)) {
return null;
}
const schema = o.content["application/json"].schema;

if ("$ref" in schema) {
return prefix + refToSchemaName(schema.$ref);
}

if (schema.type === "array") {
if ("$ref" in schema.items) {
return prefix + refToSchemaName(schema.items.$ref) + "[]";
}
return null;
}

return null;
}

/**
Expand Down
22 changes: 8 additions & 14 deletions generator/client/msw-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import type { OpenAPIV3 } from "openapi-types";
import { initIO } from "../io";
import { refToSchemaName } from "../schema/base";
import { snakeToCamel, snakeToPascal } from "../util";
import { contentRef, iterPathConfig } from "./base";
import path from "node:path";
Expand Down Expand Up @@ -83,13 +82,9 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
conf.responses["202"] ||
conf.responses["204"];

const successTypeRef = contentRef(successResponse);
const successType = successTypeRef
? "Api." + refToSchemaName(successTypeRef)
: "void";
const successType = contentRef(successResponse, "Api.");

const bodyTypeRef = contentRef(conf.requestBody);
const bodyType = bodyTypeRef ? refToSchemaName(bodyTypeRef) : null;
const bodyType = contentRef(conf.requestBody);
const body =
bodyType && (method === "post" || method === "put")
? `body: Json<Api.${bodyType}>,`
Expand All @@ -108,10 +103,9 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
: "";
const params = `params: { ${pathParamsType} ${queryParamsType} ${body} req: Request, cookies: Record<string, string> }`;

const resultType =
successType === "void"
? "Promisable<StatusCode>"
: `Promisable<HandlerResult<${successType}>>`;
const resultType = successType
? `Promisable<HandlerResult<${successType}>>`
: "Promisable<StatusCode>";

w(`/** \`${method.toUpperCase()} ${formatPath(path)}\` */`);
w(` ${opName}: (${params}) => ${resultType},`);
Expand Down Expand Up @@ -210,10 +204,10 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
return [`);
for (const { path, method, opId, conf } of iterPathConfig(spec.paths)) {
const handler = snakeToCamel(opId);
const bodyTypeRef = contentRef(conf.requestBody);
const bodyType = contentRef(conf.requestBody, "schema.");
const bodySchema =
bodyTypeRef && (method === "post" || method === "put")
? `schema.${refToSchemaName(bodyTypeRef)}`
bodyType !== "void" && (method === "post" || method === "put")
? bodyType
: "null";
const paramSchema = conf.parameters?.length
? `schema.${snakeToPascal(opId)}Params`
Expand Down

0 comments on commit 4217d12

Please sign in to comment.