-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial changes in generator to support Zod #73
- Loading branch information
Showing
22 changed files
with
841 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
267 changes: 267 additions & 0 deletions
267
packages/generator/__tests__/openapi/openApi2Parser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
import type { OpenAPIV2 } from "openapi-types"; | ||
import { describe, expect, it } from "vitest"; | ||
import AliasEntity from "../../src/openapi/models/aliasEntity"; | ||
import Enum from "../../src/openapi/models/enum"; | ||
import ObjectEntity from "../../src/openapi/models/objectEntity"; | ||
import OpenApi2Parser from "../../src/openapi/parsers/openApi2Parser"; | ||
|
||
function createParser(definitions?: OpenAPIV2.DefinitionsObject) { | ||
return new OpenApi2Parser({ definitions } as any); | ||
} | ||
|
||
describe("OpenApi2Parser", () => { | ||
describe("arrays", () => { | ||
it("parses inlined array with value items", () => { | ||
const parser = createParser({ | ||
ParentEntity: { | ||
type: "object", | ||
properties: { | ||
collection: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
parser.parse(); | ||
|
||
const parentEntity = parser.types.get("ParentEntity"); | ||
expect(parentEntity).toBeDefined(); | ||
|
||
const collection = parser.types.get("ParentEntityCollection"); | ||
expect(collection).toBeDefined(); | ||
|
||
expect((parentEntity?.type as ObjectEntity).properties.find(x => x.name === "collection")?.type).toBe(collection); | ||
|
||
const item = parser.types.get("ParentEntityCollectionItem"); | ||
expect(item).toBeDefined(); | ||
|
||
expect(collection?.type as AliasEntity).toMatchObject({ isArray: true, referencedEntity: item }); | ||
}); | ||
|
||
it("parses inlined array with inlined items", () => { | ||
const parser = createParser({ | ||
ParentEntity: { | ||
type: "object", | ||
properties: { | ||
collection: { | ||
type: "array", | ||
items: { | ||
type: "object", | ||
properties: { | ||
firstName: { type: "string" }, | ||
lastName: { type: "string" }, | ||
}, | ||
} as any, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
parser.parse(); | ||
|
||
const parentEntity = parser.types.get("ParentEntity"); | ||
expect(parentEntity).toBeDefined(); | ||
|
||
const collection = parser.types.get("ParentEntityCollection"); | ||
expect(collection).toBeDefined(); | ||
|
||
expect((parentEntity?.type as ObjectEntity).properties.find(x => x.name === "collection")?.type).toBe(collection); | ||
|
||
const item = parser.types.get("ParentEntityCollectionItem"); | ||
expect(item).toBeDefined(); | ||
|
||
expect(collection?.type as AliasEntity).toMatchObject({ isArray: true, referencedEntity: item }); | ||
}); | ||
|
||
it("parses inlined array with referenced items", () => { | ||
const parser = createParser({ | ||
ParentEntity: { | ||
type: "object", | ||
properties: { | ||
collection: { | ||
type: "array", | ||
items: { $ref: "#/definitions/Item" }, | ||
}, | ||
}, | ||
}, | ||
Item: { | ||
type: "object", | ||
properties: { | ||
firstName: { type: "string" }, | ||
lastName: { type: "string" }, | ||
}, | ||
}, | ||
}); | ||
|
||
parser.parse(); | ||
|
||
const parentEntity = parser.types.get("ParentEntity"); | ||
expect(parentEntity).toBeDefined(); | ||
|
||
const collection = parser.types.get("ParentEntityCollection"); | ||
expect(collection).toBeDefined(); | ||
|
||
expect((parentEntity?.type as ObjectEntity).properties.find(x => x.name === "collection")?.type).toBe(collection); | ||
|
||
const item = parser.types.get("Item"); | ||
expect(item).toBeDefined(); | ||
|
||
expect(collection?.type as AliasEntity).toMatchObject({ isArray: true, referencedEntity: item }); | ||
}); | ||
|
||
it("parses references array with inlined items", () => { | ||
const parser = createParser({ | ||
ParentEntity: { | ||
type: "object", | ||
properties: { | ||
collection: { $ref: "#/definitions/ItemsList" }, | ||
}, | ||
}, | ||
ItemsList: { | ||
type: "array", | ||
items: { | ||
type: "object", | ||
properties: { | ||
firstName: { type: "string" }, | ||
lastName: { type: "string" }, | ||
}, | ||
} as any, | ||
}, | ||
}); | ||
|
||
parser.parse(); | ||
|
||
const parentEntity = parser.types.get("ParentEntity"); | ||
expect(parentEntity).toBeDefined(); | ||
|
||
const collection = parser.types.get("ItemsList"); | ||
expect(collection).toBeDefined(); | ||
|
||
expect((parentEntity?.type as ObjectEntity).properties.find(x => x.name === "collection")?.type).toBe(collection); | ||
|
||
const item = parser.types.get("ItemsListItem"); | ||
expect(item).toBeDefined(); | ||
|
||
expect(collection?.type as AliasEntity).toMatchObject({ isArray: true, referencedEntity: item }); | ||
}); | ||
|
||
it("parses references array with referenced items", () => { | ||
const parser = createParser({ | ||
ParentEntity: { | ||
type: "object", | ||
properties: { | ||
collection: { $ref: "#/definitions/ItemsList" }, | ||
}, | ||
}, | ||
ItemsList: { | ||
type: "array", | ||
items: { $ref: "#/definitions/Item" }, | ||
}, | ||
Item: { | ||
type: "object", | ||
properties: { | ||
firstName: { type: "string" }, | ||
lastName: { type: "string" }, | ||
}, | ||
}, | ||
}); | ||
|
||
parser.parse(); | ||
|
||
const parentEntity = parser.types.get("ParentEntity"); | ||
expect(parentEntity).toBeDefined(); | ||
|
||
const collection = parser.types.get("ItemsList"); | ||
expect(collection).toBeDefined(); | ||
|
||
expect((parentEntity?.type as ObjectEntity).properties.find(x => x.name === "collection")?.type).toBe(collection); | ||
|
||
const item = parser.types.get("Item"); | ||
expect(item).toBeDefined(); | ||
|
||
expect(collection?.type as AliasEntity).toMatchObject({ isArray: true, referencedEntity: item }); | ||
}); | ||
}); | ||
|
||
describe("parseSchemaObject", () => { | ||
it("returns simple type", () => { | ||
const definition: OpenAPIV2.SchemaObject = { | ||
type: "integer", | ||
}; | ||
|
||
const { type } = createParser().parseSchemaObject("MyType", definition); | ||
|
||
expect(type).toBe("number"); | ||
}); | ||
|
||
it("generates a placeholder for reference object", () => { | ||
const definition: OpenAPIV2.SchemaObject = { | ||
$ref: "#/definitions/Category", | ||
}; | ||
|
||
const { type } = createParser().parseSchemaObject("MyType", definition); | ||
expect(type).toBeUndefined(); | ||
}); | ||
|
||
it("returns object entity", () => { | ||
const definition: OpenAPIV2.SchemaObject = { | ||
type: "object", | ||
properties: { foo: { type: "integer" }, bar: { type: "string" } }, | ||
}; | ||
|
||
const parser = createParser(); | ||
const { type } = parser.parseSchemaObject("MyType", definition); | ||
expect(type).toBeInstanceOf(ObjectEntity); | ||
|
||
expect(type).toMatchObject({ | ||
name: "MyType", | ||
properties: [ | ||
{ name: "foo", type: { type: "number" } }, | ||
{ name: "bar", type: { type: "string" } }, | ||
], | ||
}); | ||
}); | ||
|
||
it("returns array of simple types", () => { | ||
const definition: OpenAPIV2.SchemaObject = { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
}, | ||
}; | ||
|
||
const { type } = createParser().parseSchemaObject("MyType", definition); | ||
expect(type).toBeInstanceOf(AliasEntity); | ||
expect(type).toMatchObject({ isArray: true, referencedEntity: { type: "string" } }); | ||
}); | ||
|
||
it("returns array of entity references", () => { | ||
const definition: OpenAPIV2.SchemaObject = { | ||
type: "array", | ||
items: { | ||
type: "any", | ||
$ref: "#/definitions/Tag", | ||
}, | ||
}; | ||
|
||
const { type } = createParser().parseSchemaObject("MyType", definition); | ||
expect(type).toBeInstanceOf(AliasEntity); | ||
expect(type).toMatchObject({ isArray: true, referencedEntity: { type: undefined } }); | ||
}); | ||
|
||
it("returns enum", () => { | ||
const definition: OpenAPIV2.SchemaObject = { | ||
type: "string", | ||
enum: ["one", "two", "three"], | ||
}; | ||
|
||
const { type } = createParser().parseSchemaObject("MyType", definition); | ||
expect(type).toBeInstanceOf(Enum); | ||
expect(type).toMatchObject({ items: ["one", "two", "three"] }); | ||
}); | ||
}); | ||
}); |
91 changes: 0 additions & 91 deletions
91
packages/generator/__tests__/openapi/openApi2Parser.tests.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.