Skip to content

Commit

Permalink
Initial changes in generator to support Zod #73
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Jul 24, 2022
1 parent 02def8e commit 4411eff
Show file tree
Hide file tree
Showing 22 changed files with 841 additions and 398 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"mobx": "5",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-node": "^10.9.1",
"typedoc": "^0.23.2",
"vite": "^2.9.13",
"vitest": "^0.16.0",
Expand Down
267 changes: 267 additions & 0 deletions packages/generator/__tests__/openapi/openApi2Parser.test.ts
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 packages/generator/__tests__/openapi/openApi2Parser.tests.ts

This file was deleted.

Loading

0 comments on commit 4411eff

Please sign in to comment.