Skip to content

Commit

Permalink
feat: additionalProperties #1
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Oct 12, 2024
1 parent 671bc22 commit df918bf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/petstore-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class ApiClient {

store = {
getInventory: () => {
return this.Fetch<object>("get", "/store/inventory", {})
return this.Fetch<Record<string, number>>("get", "/store/inventory", {})
},

placeOrder: (body: Order) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class ApiClient {

store = {
getInventory: () => {
return this.Fetch<object>("get", "/store/inventory", {})
return this.Fetch<Record<string, number>>("get", "/store/inventory", {})
},

placeOrder: (body: Order) => {
Expand Down
17 changes: 15 additions & 2 deletions src/type-gen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Oas3Schema, Referenced } from "@redocly/openapi-core/lib/typings/openapi"
import { filterEmpty } from "array-utils-ts"
import { isArray, uniq, upperFirst } from "lodash-es"
import { isArray, isBoolean, uniq, upperFirst } from "lodash-es"
import ts from "typescript"
import { Context } from "./config"
import { OAS3, unref } from "./schema"
Expand Down Expand Up @@ -68,6 +68,19 @@ const makeInlineEnum = (s: OAS3) => {
return undefined
}

const makeObject = (ctx: Context, s: OAS3): ts.TypeNode => {
if (s.type !== "object") throw new Error(`makeObject: not an object ${JSON.stringify(s)}`)

if (s.additionalProperties && !isBoolean(s.additionalProperties)) {
return f.createTypeReferenceNode("Record", [
f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
makeType(ctx, s.additionalProperties),
])
}

return f.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword)
}

export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
const mk = makeType.bind(null, ctx)

Expand Down Expand Up @@ -123,7 +136,7 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {

let t: ts.TypeNode
// if (s.type === "object") t = f.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
if (s.type === "object") t = f.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword)
if (s.type === "object") t = makeObject(ctx, s)
else if (s.type === "boolean") t = f.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword)
else if (s.type === "number") t = f.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)
else if (s.type === "string") t = f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
Expand Down
13 changes: 13 additions & 0 deletions test/type-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ test("type inline", async () => {
{ type: "object", properties: { a: { type: "string" } }, additionalProperties: true },
"{ a?: string }",
)

t({ type: "object", additionalProperties: { type: "number" } }, "Record<string, number>")

t(
{
type: "object",
additionalProperties: {
type: "array",
items: { oneOf: [{ type: "string" }, { type: "number" }] },
},
},
"Record<string, (string | number)[]>",
)
})

test("type alias", async () => {
Expand Down

0 comments on commit df918bf

Please sign in to comment.