-
Notifications
You must be signed in to change notification settings - Fork 0
Feature implementation from commits 89b41b5..494583f #4
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
base: feature-base-branch-2
Are you sure you want to change the base?
Changes from all commits
04dd1b5
989320e
def55df
197316b
f0672b5
8b710ff
aa4c7a3
9bc5164
8d84b02
7224486
f3ee25e
c574b50
e812354
3ec4915
494583f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
import type { OpenAPIObject, OperationObject, PathItemObject, SchemaObject } from "openapi3-ts"; | ||||||
import type { OpenAPIObject, OperationObject, PathItemObject, ReferenceObject, SchemaObject } from "openapi3-ts"; | ||||||
import { sortBy, sortListFromRefArray, sortObjKeysFromArray } from "pastable/server"; | ||||||
import { ts } from "tanu"; | ||||||
import { match } from "ts-pattern"; | ||||||
|
@@ -11,6 +11,7 @@ import { getTypescriptFromOpenApi } from "./openApiToTypescript"; | |||||
import { getZodSchema } from "./openApiToZod"; | ||||||
import { topologicalSort } from "./topologicalSort"; | ||||||
import { asComponentSchema, normalizeString } from "./utils"; | ||||||
import type { CodeMetaData } from "./CodeMeta"; | ||||||
|
||||||
const file = ts.createSourceFile("", "", ts.ScriptTarget.ESNext, true); | ||||||
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); | ||||||
|
@@ -138,7 +139,9 @@ export const getZodClientTemplateContext = ( | |||||
const addDependencyIfNeeded = (schemaName: string) => { | ||||||
if (!schemaName) return; | ||||||
if (schemaName.startsWith("z.")) return; | ||||||
dependencies.add(schemaName); | ||||||
// Sometimes the schema includes a chain that should be removed from the dependency | ||||||
const [normalizedSchemaName] = schemaName.split("."); | ||||||
dependencies.add(normalizedSchemaName!); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Correctness Issue Non-null Assertion on Potentially Undefined Value. The non-null assertion operator on normalizedSchemaName could cause runtime errors if the split pattern produces unexpected results. Current Code (Diff): - dependencies.add(normalizedSchemaName!);
+ dependencies.add(normalizedSchemaName || schemaName); 📝 Committable suggestion
Suggested change
|
||||||
}; | ||||||
|
||||||
addDependencyIfNeeded(endpoint.response); | ||||||
|
@@ -394,11 +397,16 @@ export type TemplateContextOptions = { | |||||
* When true, returns a "responses" array with all responses (both success and errors) | ||||||
*/ | ||||||
withAllResponses?: boolean; | ||||||
|
||||||
/** | ||||||
* When true, prevents using the exact same name for the same type | ||||||
* For example, if 2 schemas have the same type, but different names, export each as separate schemas | ||||||
* If 2 schemas have the same name but different types, export subsequent names with numbers appended | ||||||
*/ | ||||||
exportAllNamedSchemas?: boolean; | ||||||
|
||||||
/** | ||||||
* A function that runs in the schema conversion process to refine the schema before it's converted to a Zod schema. | ||||||
*/ | ||||||
schemaRefiner?: <T extends SchemaObject | ReferenceObject>(schema: T, parentMeta?: CodeMetaData) => T; | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { OpenAPIObject } from "openapi3-ts"; | ||
import { expect, test } from "vitest"; | ||
import { generateZodClientFromOpenAPI } from "../src"; | ||
|
||
test("array-body-with-chains-tag-group-strategy", async () => { | ||
const openApiDoc: OpenAPIObject = { | ||
openapi: "3.0.0", | ||
info: { title: "Test", version: "1.0.1" }, | ||
paths: { | ||
"/test": { | ||
put: { | ||
summary: "Test", | ||
description: "Test", | ||
tags: ["Test"], | ||
requestBody: { | ||
content: { | ||
"application/json": { | ||
schema: { | ||
type: "array", | ||
items: { | ||
type: "object", | ||
properties: { | ||
testItem: { | ||
type: "string", | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
minItems: 1, | ||
maxItems: 10, | ||
}, | ||
}, | ||
}, | ||
}, | ||
parameters: [], | ||
responses: { | ||
"200": { | ||
description: "Success", | ||
content: { "application/json": {} }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
components: {}, | ||
tags: [], | ||
}; | ||
|
||
const output = await generateZodClientFromOpenAPI({ | ||
disableWriteToFile: true, | ||
openApiDoc, | ||
options: { groupStrategy: "tag-file" }, | ||
}); | ||
expect(output).toMatchInlineSnapshot(` | ||
{ | ||
"Test": "import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core"; | ||
import { z } from "zod"; | ||
|
||
const putTest_Body = z.array(z.object({ testItem: z.string() }).partial()); | ||
|
||
export const schemas = { | ||
putTest_Body, | ||
}; | ||
|
||
const endpoints = makeApi([ | ||
{ | ||
method: "put", | ||
path: "/test", | ||
description: \`Test\`, | ||
requestFormat: "json", | ||
parameters: [ | ||
{ | ||
name: "body", | ||
type: "Body", | ||
schema: putTest_Body.min(1).max(10), | ||
}, | ||
], | ||
response: z.void(), | ||
}, | ||
]); | ||
|
||
export const TestApi = new Zodios(endpoints); | ||
|
||
export function createApiClient(baseUrl: string, options?: ZodiosOptions) { | ||
return new Zodios(baseUrl, endpoints, options); | ||
} | ||
", | ||
"__index": "export { TestApi } from "./Test"; | ||
", | ||
} | ||
`); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 Correctness Issue
Property name mismatch with type definition.
The code initializes ctx.schemasByName but the type definition uses schemaByName (without 's'), which will cause runtime errors when accessing this property.
Current Code (Diff):
📝 Committable suggestion
🔄 Dependencies Affected
lib/src/template-context.ts
Function:
any function using getZodiosEndpointDefinitionList result
Issue: Code expecting schemaByName property but receiving schemasByName
Suggestion: Update any code using ctx.schemasByName to use ctx.schemaByName instead