Skip to content

Commit

Permalink
fix openapi type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
omermecitoglu committed Jun 13, 2024
1 parent ae1bdf4 commit e5f88a2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/core/resolvers/imported-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function resolveSchemas(paths: PathsObject) {
}

function resolvePropDefinition(definition: SchemaObject) {
if (definition.$ref) {
if ("$ref" in definition) {
return [definition.$ref.replace("#/components/schemas/", "")];
}
if (definition.type === "array") {
Expand Down
2 changes: 1 addition & 1 deletion src/core/resolvers/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function resolveProperties(collection: Record<string, SchemaObjec
`${propertyName}${isRequired ? "" : "?"}:`,
`${resolveSchemaWithNull(property)},`,
];
if (property.readOnly) content.unshift("readonly");
if ("readOnly" in property && property.readOnly) content.unshift("readonly");
return {
content: content.join(" "),
description: property.description || "missing description",
Expand Down
52 changes: 22 additions & 30 deletions src/core/resolvers/schema-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,32 @@ function resolveTuple(items: SchemaObject | SchemaObject[], length: number) {

export function resolveSchema(definition?: SchemaObject): string {
if (!definition) return "unknown";
if (definition.$ref) {
if ("$ref" in definition) {
return definition.$ref.replace("#/components/schemas/", "");
}
if (definition.type) {
switch (definition.type) {
case "string": {
if (definition.format === "binary") return "File";
// TODO: handle definition.format === "date"
if (definition.enum) {
return definition.enum.map(resolveEnumItem).join(" | ");
}
return "string";
switch (definition.type) {
case "string": {
if (definition.format === "binary") return "File";
// TODO: handle definition.format === "date"
if (definition.enum) {
return definition.enum.map(resolveEnumItem).join(" | ");
}
case "number": return "number";
case "boolean": return "boolean";
case "array": {
if (definition.items) {
if (definition.maxItems && definition.maxItems === definition.minItems) {
return resolveTuple(definition.items, definition.maxItems);
}
if (Array.isArray(definition.items)) {
return resolveArray(definition.items, true);
}
return `${resolveSchema(definition.items)}[]`;
}
return "unknown[]";
return "string";
}
case "number": return "number";
case "boolean": return "boolean";
case "array": {
if (definition.maxItems && definition.maxItems === definition.minItems) {
return resolveTuple(definition.items, definition.maxItems);
}
case "object": {
if (definition.properties) {
const props = resolveObject(definition.properties, definition.required ?? []);
return `{ ${props.join(", ")} }`;
}
return "unknown";
if (Array.isArray(definition.items)) {
return resolveArray(definition.items, true);
}
return `${resolveSchema(definition.items)}[]`;
}
case "object": {
const props = resolveObject(definition.properties, definition.required ?? []);
return `{ ${props.join(", ")} }`;
}
}
if (definition.oneOf) {
Expand All @@ -73,7 +65,7 @@ export function resolveSchema(definition?: SchemaObject): string {
}

export function resolveSchemaWithNull(definition: SchemaObject) {
if (definition.nullable) {
if ("nullable" in definition && definition.nullable) {
return `${resolveSchema(definition)} | null`;
}
return resolveSchema(definition);
Expand Down

0 comments on commit e5f88a2

Please sign in to comment.