From 4217d1203e994e08eab139cc9009771a7605c53d Mon Sep 17 00:00:00 2001 From: David Crespo Date: Fri, 5 Apr 2024 11:43:17 -0500 Subject: [PATCH] handle array response even though we shouldn't have them (#238) --- client/Api.ts | 8 ++++---- client/msw-handlers.ts | 8 ++++---- generator/client/api.ts | 11 +++-------- generator/client/base.ts | 28 ++++++++++++++++++++-------- generator/client/msw-handlers.ts | 22 ++++++++-------------- 5 files changed, 39 insertions(+), 38 deletions(-) diff --git a/client/Api.ts b/client/Api.ts index f25395b..31aa560 100644 --- a/client/Api.ts +++ b/client/Api.ts @@ -6221,7 +6221,7 @@ export class Api extends HttpClient { * Get BFD status */ networkingBfdStatus: (_: EmptyObj, params: FetchParams = {}) => { - return this.request({ + return this.request({ path: `/v1/system/networking/bfd-status`, method: "GET", ...params, @@ -6276,7 +6276,7 @@ export class Api extends HttpClient { { query }: { query?: NetworkingBgpAnnounceSetListQueryParams }, params: FetchParams = {} ) => { - return this.request({ + return this.request({ path: `/v1/system/networking/bgp-announce`, method: "GET", query, @@ -6318,7 +6318,7 @@ export class Api extends HttpClient { { query }: { query?: NetworkingBgpImportedRoutesIpv4QueryParams }, params: FetchParams = {} ) => { - return this.request({ + return this.request({ path: `/v1/system/networking/bgp-routes-ipv4`, method: "GET", query, @@ -6329,7 +6329,7 @@ export class Api extends HttpClient { * Get BGP peer status */ networkingBgpStatus: (_: EmptyObj, params: FetchParams = {}) => { - return this.request({ + return this.request({ path: `/v1/system/networking/bgp-status`, method: "GET", ...params, diff --git a/client/msw-handlers.ts b/client/msw-handlers.ts index 2a5413c..f3801c1 100644 --- a/client/msw-handlers.ts +++ b/client/msw-handlers.ts @@ -885,7 +885,7 @@ export interface MSWHandlers { networkingBfdStatus: (params: { req: Request; cookies: Record; - }) => Promisable; + }) => Promisable>; /** `GET /v1/system/networking/bgp` */ networkingBgpConfigList: (params: { query: Api.NetworkingBgpConfigListQueryParams; @@ -909,7 +909,7 @@ export interface MSWHandlers { query: Api.NetworkingBgpAnnounceSetListQueryParams; req: Request; cookies: Record; - }) => Promisable; + }) => Promisable>; /** `POST /v1/system/networking/bgp-announce` */ networkingBgpAnnounceSetCreate: (params: { body: Json; @@ -927,12 +927,12 @@ export interface MSWHandlers { query: Api.NetworkingBgpImportedRoutesIpv4QueryParams; req: Request; cookies: Record; - }) => Promisable; + }) => Promisable>; /** `GET /v1/system/networking/bgp-status` */ networkingBgpStatus: (params: { req: Request; cookies: Record; - }) => Promisable; + }) => Promisable>; /** `GET /v1/system/networking/loopback-address` */ networkingLoopbackAddressList: (params: { query: Api.NetworkingLoopbackAddressListQueryParams; diff --git a/generator/client/api.ts b/generator/client/api.ts index c4a16e9..738ea8f 100644 --- a/generator/client/api.ts +++ b/generator/client/api.ts @@ -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, @@ -226,8 +225,7 @@ 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"] || @@ -235,10 +233,7 @@ export function generateApi(spec: OpenAPIV3.Document, destDir: string) { 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); @@ -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) { diff --git a/generator/client/base.ts b/generator/client/base.ts index 8557d6b..c24e42e 100644 --- a/generator/client/base.ts +++ b/generator/client/base.ts @@ -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; @@ -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; } /** diff --git a/generator/client/msw-handlers.ts b/generator/client/msw-handlers.ts index 32dabbf..356be5b 100644 --- a/generator/client/msw-handlers.ts +++ b/generator/client/msw-handlers.ts @@ -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"; @@ -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,` @@ -108,10 +103,9 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) { : ""; const params = `params: { ${pathParamsType} ${queryParamsType} ${body} req: Request, cookies: Record }`; - const resultType = - successType === "void" - ? "Promisable" - : `Promisable>`; + const resultType = successType + ? `Promisable>` + : "Promisable"; w(`/** \`${method.toUpperCase()} ${formatPath(path)}\` */`); w(` ${opName}: (${params}) => ${resultType},`); @@ -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`