Skip to content

Commit 4c21143

Browse files
committed
add generic schema check for tuples
1 parent c2283bd commit 4c21143

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/core/resolvers/imported-schema.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { OpenAPI, Operation, SchemaDefinition } from "~/core/openapi";
2+
import { getTupleItems, isTuple } from "~/core/tuple";
23
import getContentSchema from "./content";
34
import resolveEndpoints from "./enpoint";
45
import { filterGenericSchemas, resolveSchema, simplifySchema } from "./schema-definition";
@@ -16,12 +17,21 @@ function resolveResponseSchemas(responses: Operation["responses"]) {
1617
}).flat();
1718
}
1819

20+
function extractTuples(collection: string[]) {
21+
return collection.map(schema => {
22+
if (isTuple(schema.replaceAll("[]", ""))) {
23+
return getTupleItems(schema);
24+
}
25+
return schema;
26+
}).flat();
27+
}
28+
1929
export function resolveSchemas(paths: OpenAPI["paths"]) {
2030
const collection = resolveEndpoints(paths).map(({ operation }) => ([
2131
...resolveRequestSchemas(operation.requestBody),
2232
...resolveResponseSchemas(operation.responses),
2333
])).flat();
24-
const uniqueCollection = Array.from(new Set(collection));
34+
const uniqueCollection = Array.from(new Set(extractTuples(collection)));
2535
return filterGenericSchemas(uniqueCollection).toSorted();
2636
}
2737

@@ -43,6 +53,6 @@ function resolvePropDefinition(definition: SchemaDefinition) {
4353

4454
export function resolveSchemasFromProps(props: Record<string, SchemaDefinition>) {
4555
const collection = Object.values(props).map(resolvePropDefinition).flat();
46-
const uniqueCollection = Array.from(new Set(collection));
56+
const uniqueCollection = Array.from(new Set(extractTuples(collection)));
4757
return filterGenericSchemas(uniqueCollection).toSorted();
4858
}

src/core/resolvers/schema-definition.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { SchemaDefinition } from "~/core/openapi";
2+
import { getTupleItems, isTuple } from "~/core/tuple";
23

34
function resolveArray(items: SchemaDefinition[], isArray: boolean) {
45
const schemas = items.map(resolveSchema);
@@ -95,13 +96,18 @@ function isEverydayType(schema: string) {
9596
}
9697

9798
function isArraySchema(schema: string) {
98-
return schema.includes("[]");
99+
return schema.endsWith("[]");
99100
}
100101

101102
function isGenericSchema(schema: string) {
102103
if (isArraySchema(schema)) {
103104
return isGenericSchema(schema.replace("[]", ""));
104105
}
106+
if (isTuple(schema)) {
107+
const items = getTupleItems(schema);
108+
if (items.every(isGenericSchema)) return true;
109+
throw new Error("There is a named type in the tuple. This resolver is not smart enough to handle that.");
110+
}
105111
if (isEverydayType(schema)) return true;
106112
if (isRawObject(schema)) return true;
107113
return false;

src/core/tuple.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function isTuple(schema: string) {
2+
return (/^\[(?:\s*[^,[\]]+\s*(?:,\s*[^,[\]]+\s*)*)?\]$/).test(schema);
3+
}
4+
5+
export function getTupleItems(schema: string) {
6+
const matches = schema.match(/\s*([^,[\]\s]+)\s*/g);
7+
return matches?.map(m => m.trim()) ?? [];
8+
}

0 commit comments

Comments
 (0)