import { convertFromJsonSchemaToInputSchema } from "../convertFromJsonSchemaToInputSchema"; import { InputJsonSchema } from "../InputJsonSchema"; import { InputProperty } from "Schema/InputSchema"; describe("convertFromJsonSchemaToInputSchema", () => { test("can convert simple object properties", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myProperty: { type: "string" }, }, required: ["myProperty"], }, }, }; const inputs = { myProperty: "foo", }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myProperty", isRequired: true, type: { type: "string" }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert simple object properties that aren't required", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myProperty: { type: "string" }, mySecondProperty: { type: "string" }, }, required: ["mySecondProperty"], }, }, }; const inputs = { myProperty: "foo", }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedFirstProperty: InputProperty = { name: "myProperty", isRequired: false, type: { type: "string" }, }; const expectedSecondProperty: InputProperty = { name: "mySecondProperty", isRequired: true, type: { type: "string" }, }; expect(schema.properties).toEqual([expectedFirstProperty, expectedSecondProperty]); }); test("can convert when no required array is provided in json schema", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myProperty: { type: "string" }, }, }, }, }; const inputs = { myProperty: "foo", }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myProperty", isRequired: false, type: { type: "string" }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert package references", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myPackageReference: { type: "object", isPackageReference: true }, }, required: ["myPackageReference"], }, }, }; const inputs = { myPackageReference: { feedId: undefined, packageId: undefined }, }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myPackageReference", isRequired: true, type: { type: "package" }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert accounts", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myAccount: { type: "string", isAccount: true, accountType: "AmazonWebServicesAccount" }, }, required: ["myAccount"], }, }, }; const inputs = { myAccount: "Account1", }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myAccount", isRequired: true, type: { type: "account", accountType: "AmazonWebServicesAccount" }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert union types", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myUnionType: { anyOf: [{ $ref: "FirstType" }, { $ref: "SecondType" }] }, }, required: ["myUnionType"], }, FirstType: { type: "object", properties: { type: { type: "string", const: "first", isDiscriminator: true }, first: { type: "boolean" }, }, required: ["type", "first"], }, SecondType: { type: "object", properties: { type: { type: "string", const: "second", isDiscriminator: true }, second: { type: "object", isSensitiveValue: true }, }, required: ["type", "second"], }, }, }; const inputs = { myUnionType: { type: "first", first: true, }, }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myUnionType", isRequired: true, type: { type: "object", discriminatorProperties: [ { discriminatorName: "type", type: { type: "discriminator", const: "first" }, }, ], nonDiscriminatorProperties: [ { name: "first", isRequired: true, type: { type: "primitive" }, }, ], }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert union types with correct required properties", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myUnionType: { anyOf: [{ $ref: "FirstType" }, { $ref: "SecondType" }] }, }, required: ["myUnionType"], }, FirstType: { type: "object", properties: { type: { type: "string", const: "first", isDiscriminator: true }, first: { type: "boolean" }, }, required: ["type"], }, SecondType: { type: "object", properties: { type: { type: "string", const: "second", isDiscriminator: true }, second: { type: "object", isSensitiveValue: true }, }, required: ["type", "second"], }, }, }; const inputs = { myUnionType: { type: "first", first: true, }, }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myUnionType", isRequired: true, type: { type: "object", discriminatorProperties: [ { discriminatorName: "type", type: { type: "discriminator", const: "first" }, }, ], nonDiscriminatorProperties: [ { name: "first", isRequired: false, type: { type: "primitive" }, }, ], }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("flattens simple type references", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myProperty: { $ref: "SomeStringRef" }, }, required: ["myProperty"], }, SomeStringRef: { $ref: "SomeString" }, SomeString: { type: "string" }, }, }; const inputs = { myProperty: "foo", }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myProperty", isRequired: true, type: { type: "string" }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("flattens union type references", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myUnionType: { anyOf: [{ $ref: "FirstTypeRef" }, { $ref: "SecondOrThirdType" }] }, }, required: ["myUnionType"], }, FirstTypeRef: { $ref: "FirstType", }, FirstType: { type: "object", properties: { type: { type: "string", const: "first", isDiscriminator: true }, first: { type: "boolean" }, }, required: ["first"], }, SecondOrThirdType: { anyOf: [{ $ref: "SecondType" }, { $ref: "ThirdType" }], }, SecondType: { type: "object", properties: { type: { type: "string", const: "second", isDiscriminator: true }, second: { type: "object", isSensitiveValue: true }, }, required: ["second"], }, ThirdType: { type: "object", properties: { type: { type: "string", const: "third", isDiscriminator: true }, third: { type: "object", isPackageReference: true }, }, required: ["third"], }, }, }; const inputs = { myUnionType: { type: "second", second: { type: "empty" }, }, }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myUnionType", isRequired: true, type: { type: "object", discriminatorProperties: [ { discriminatorName: "type", type: { type: "discriminator", const: "second" }, }, ], nonDiscriminatorProperties: [ { name: "second", isRequired: true, type: { type: "sensitive" }, }, ], }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("flattens union type references with correct required properties", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myUnionType: { anyOf: [{ $ref: "FirstTypeRef" }, { $ref: "SecondOrThirdType" }] }, }, required: ["myUnionType"], }, FirstTypeRef: { $ref: "FirstType", }, FirstType: { type: "object", properties: { type: { type: "string", const: "first", isDiscriminator: true }, first: { type: "boolean" }, }, required: ["first"], }, SecondOrThirdType: { anyOf: [{ $ref: "SecondType" }, { $ref: "ThirdType" }], }, SecondType: { type: "object", properties: { type: { type: "string", const: "second", isDiscriminator: true }, second: { type: "object", isSensitiveValue: true }, }, required: [], }, ThirdType: { type: "object", properties: { type: { type: "string", const: "third", isDiscriminator: true }, third: { type: "object", isPackageReference: true }, }, required: ["third"], }, }, }; const inputs = { myUnionType: { type: "second", second: { type: "empty" }, }, }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myUnionType", isRequired: true, type: { type: "object", discriminatorProperties: [ { discriminatorName: "type", type: { type: "discriminator", const: "second" }, }, ], nonDiscriminatorProperties: [ { name: "second", isRequired: false, type: { type: "sensitive" }, }, ], }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert arrays", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myArray: { type: "array", items: { $ref: "ItemType" } }, }, required: ["myArray"], }, ItemType: { type: "boolean" }, }, }; const inputs = { myArray: [true, false], }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myArray", isRequired: true, type: { type: "array", itemTypes: [{ type: "primitive" }, { type: "primitive" }] }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert arrays of object types with required properties", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myArray: { type: "array", items: { $ref: "ItemType" } }, }, required: ["myArray"], }, ItemType: { type: "object", properties: { myProperty: { type: "string" } }, required: ["myProperty"] }, }, }; const inputs = { myArray: [{ myProperty: "first" }, { myProperty: "second" }], }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myArray", isRequired: true, type: { type: "array", itemTypes: [ { type: "object", discriminatorProperties: [], nonDiscriminatorProperties: [{ type: { type: "string" }, name: "myProperty", isRequired: true }], }, { type: "object", discriminatorProperties: [], nonDiscriminatorProperties: [{ type: { type: "string" }, name: "myProperty", isRequired: true }], }, ], }, }; expect(schema.properties).toEqual([expectedProperty]); }); test("can convert arrays of object types with non required properties", () => { const inputJsonSchema: InputJsonSchema = { definitions: { MyInputs: { type: "object", isRoot: true, properties: { myArray: { type: "array", items: { $ref: "ItemType" } }, }, required: ["myArray"], }, ItemType: { type: "object", properties: { myProperty: { type: "string" } }, required: [] }, }, }; const inputs = { myArray: [{ myProperty: "first" }, { myProperty: "second" }], }; const schema = convertFromJsonSchemaToInputSchema(inputJsonSchema, inputs); const expectedProperty: InputProperty = { name: "myArray", isRequired: true, type: { type: "array", itemTypes: [ { type: "object", discriminatorProperties: [], nonDiscriminatorProperties: [{ type: { type: "string" }, name: "myProperty", isRequired: false }], }, { type: "object", discriminatorProperties: [], nonDiscriminatorProperties: [{ type: { type: "string" }, name: "myProperty", isRequired: false }], }, ], }, }; expect(schema.properties).toEqual([expectedProperty]); }); });