Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __mocks__/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ definitions:
type: object
properties:
description:
type: string
type: string
enabled:
type: boolean
enum:
Expand Down
60 changes: 45 additions & 15 deletions e2e/src/__tests__/test-api-v3/definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ import { DisjointUnionsUserTest } from "../../generated/testapiV3/DisjointUnions
import { EnabledUserTest } from "../../generated/testapiV3/EnabledUserTest";
import { EnumFalseTest } from "../../generated/testapiV3/EnumFalseTest";
import { EnumTrueTest } from "../../generated/testapiV3/EnumTrueTest";
import {AllOfWithOneElementTest} from "../../generated/testapiV3/AllOfWithOneElementTest";
import {AllOfWithOneRefElementTest} from "../../generated/testapiV3/AllOfWithOneRefElementTest";
import { AllOfWithOneElementTest } from "../../generated/testapiV3/AllOfWithOneElementTest";
import { AllOfWithOneRefElementTest } from "../../generated/testapiV3/AllOfWithOneRefElementTest";

import * as E from "fp-ts/lib/Either";
import {
PreferredLanguage,
PreferredLanguageEnum
} from "../../generated/testapi/PreferredLanguage";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";

const { generatedFilesDir, isSpecEnabled } = config.specs.testapiV3;

Expand Down Expand Up @@ -324,24 +329,48 @@ describe("EnumFalseTest definition", () => {
});
});

describe("AllOfWithOneElementTest definition", () => {
describe("ExtensiblePreferredLanguage definition", () => {
const l1Ok = PreferredLanguageEnum.de_DE;
const l2Extensible = "NON_EMPY" as NonEmptyString;
const l3Ko = 10;
const l4Ko = "";

it("should decode extensible enum with definied enum value", () => {
const result = PreferredLanguage.decode(l1Ok);
expect(E.isRight(result)).toBe(true);
});

it("should decode extensible enum with string value", () => {
const result = PreferredLanguage.decode(l2Extensible);
expect(E.isRight(result)).toBe(true);
});

it("should fail decode extensible enum with invalid value", () => {
const result = PreferredLanguage.decode(l3Ko);
expect(E.isLeft(result)).toBe(true);
});

it("should fail decode extensible enum with empty string", () => {
const result = PreferredLanguage.decode(l4Ko);
expect(E.isLeft(result)).toBe(true);
});
});

const okElement = {key: "string"};
const notOkElement = {key: 1};
describe("AllOfWithOneElementTest definition", () => {
const okElement = { key: "string" };
const notOkElement = { key: 1 };

it("Should return a right", () => {
expect(E.isRight(AllOfWithOneElementTest.decode(okElement))).toBeTruthy();
})
});

it("Should return a left", () => {
expect(E.isLeft(AllOfWithOneElementTest.decode(notOkElement))).toBeTruthy();
})

})
});
});

describe("AllOfWithOneRefElementTest", () => {

const basicProfile = {
const basicProfile = {
family_name: "Rossi",
fiscal_code: "RSSMRA80A01F205X",
has_profile: true,
Expand All @@ -351,10 +380,11 @@ const basicProfile = {
};

it("Should return a right", () => {
expect(E.isRight(AllOfWithOneRefElementTest.decode(basicProfile))).toBeTruthy();
})

})
expect(
E.isRight(AllOfWithOneRefElementTest.decode(basicProfile))
).toBeTruthy();
});
});

describe("DisjointUnionsUserTest definition", () => {
const enabledUser = {
Expand Down
73 changes: 50 additions & 23 deletions e2e/src/__tests__/test-api/definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import { AllOfWithOneElementTest } from "../../generated/testapi/AllOfWithOneEle
import { AllOfWithOneRefElementTest } from "../../generated/testapi/AllOfWithOneRefElementTest";
import { AdditionalPropsTest } from "../../generated/testapi/AdditionalPropsTest";


import * as E from "fp-ts/lib/Either"
import * as E from "fp-ts/lib/Either";
import {
PreferredLanguage,
PreferredLanguageEnum
} from "../../generated/testapi/PreferredLanguage";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";

const { generatedFilesDir } = config.specs.testapi;

Expand Down Expand Up @@ -326,43 +330,65 @@ describe("EnumFalseTest definition", () => {
});
});

describe("AllOfWithOneElementTest definition", () => {
describe("ExtensiblePreferredLanguage definition", () => {
const l1Ok = PreferredLanguageEnum.de_DE;
const l2Extensible = "NON_EMPY" as NonEmptyString;
const l3Ko = 10;
const l4Ko = "";

it("should decode extensible enum with definied enum value", () => {
const result = PreferredLanguage.decode(l1Ok);
expect(E.isRight(result)).toBe(true);
});

it("should decode extensible enum with string value", () => {
const result = PreferredLanguage.decode(l2Extensible);
expect(E.isRight(result)).toBe(true);
});

const okElement = {key: "string"};
const notOkElement = {key: 1};
it("should fail decode extensible enum with invalid value", () => {
const result = PreferredLanguage.decode(l3Ko);
expect(E.isLeft(result)).toBe(true);
});

it("should fail decode extensible enum with empty string", () => {
const result = PreferredLanguage.decode(l4Ko);
expect(E.isLeft(result)).toBe(true);
});
});

describe("AllOfWithOneElementTest definition", () => {
const okElement = { key: "string" };
const notOkElement = { key: 1 };

it("Should return a right", () => {
expect(E.isRight(AllOfWithOneElementTest.decode(okElement))).toBeTruthy();
})
});

it("Should return a left", () => {
expect(E.isLeft(AllOfWithOneElementTest.decode(notOkElement))).toBeTruthy();
})

})
});
});

describe("AdditionalPropsTest should be an object with a string as key and an array of number as value", () => {

const okElement = {"okElementProperty": [1, 2, 3]};
const notOkElement = {"notOkElementProperty": ["1", "2", "3"]};
const okElement = { okElementProperty: [1, 2, 3] };
const notOkElement = { notOkElementProperty: ["1", "2", "3"] };

it("Should return a right with a valid type", () => {
expect(E.isRight(AdditionalPropsTest.decode(okElement))).toBeTruthy();
})
});

it("Should return a left with a non valid element", () => {
expect(E.isLeft(AdditionalPropsTest.decode(notOkElement))).toBeTruthy();
})
});

it("Should return a left with undefined input", () => {
expect(E.isLeft(AdditionalPropsTest.decode(undefined))).toBeTruthy();
})

})
});
});

describe("AllOfWithOneRefElementTest", () => {

const basicProfile = {
const basicProfile = {
family_name: "Rossi",
fiscal_code: "RSSMRA80A01F205X",
has_profile: true,
Expand All @@ -372,10 +398,11 @@ const basicProfile = {
};

it("Should return a right", () => {
expect(E.isRight(AllOfWithOneRefElementTest.decode(basicProfile))).toBeTruthy();
})

})
expect(
E.isRight(AllOfWithOneRefElementTest.decode(basicProfile))
).toBeTruthy();
});
});

describe("DisjointUnionsUserTest definition", () => {
const enabledUser = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,40 @@ export type EnumTest = t.TypeOf<typeof EnumTest>;
"
`;

exports[`Openapi V2 |> gen-api-models should handle extensible enums: extesible-enum 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
* See https://github.com/pagopa/io-utils
*/
/* eslint-disable */

import { enumType } from \\"@pagopa/ts-commons/lib/types\\";
import { NonEmptyString } from \\"@pagopa/ts-commons/lib/strings\\";
import * as t from \\"io-ts\\";

export enum PreferredLanguageEnum {
\\"it_IT\\" = \\"it_IT\\",

\\"en_GB\\" = \\"en_GB\\",

\\"es_ES\\" = \\"es_ES\\",

\\"de_DE\\" = \\"de_DE\\",

\\"fr_FR\\" = \\"fr_FR\\"
}

export type PreferredLanguage = t.TypeOf<typeof PreferredLanguage>;
export const PreferredLanguage = t.union(
[
enumType<PreferredLanguageEnum>(PreferredLanguageEnum, \\"PreferredLanguage\\"),
NonEmptyString
],
\\"ExtensiblePreferredLanguage\\"
);
"
`;

exports[`Openapi V2 |> gen-api-models should handle list of defintions 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
Expand Down Expand Up @@ -640,6 +674,7 @@ exports[`Openapi V2 |> gen-api-models should not contain a t.string but an enum:
import * as t from \\"io-ts\\";
import { Profile } from \\"./Profile\\";
import { enumType } from \\"@pagopa/ts-commons/lib/types\\";
import { NonEmptyString } from \\"@pagopa/ts-commons/lib/strings\\";

export enum StatusEnum {
\\"ACTIVATED\\" = \\"ACTIVATED\\"
Expand All @@ -651,7 +686,10 @@ export enum StatusEnum {

// required attributes
const AllOfWithXExtensibleEnum2R = t.interface({
status: enumType<StatusEnum>(StatusEnum, \\"status\\")
status: t.union(
[enumType<StatusEnum>(StatusEnum, \\"status\\"), NonEmptyString],
\\"Extensiblestatus\\"
)
});

// optional attributes
Expand Down Expand Up @@ -2295,6 +2333,40 @@ export type EnumTest = t.TypeOf<typeof EnumTest>;
"
`;

exports[`Openapi V3 |> gen-api-models should handle extensible enums: extesible-enum 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
* See https://github.com/pagopa/io-utils
*/
/* eslint-disable */

import { enumType } from \\"@pagopa/ts-commons/lib/types\\";
import { NonEmptyString } from \\"@pagopa/ts-commons/lib/strings\\";
import * as t from \\"io-ts\\";

export enum PreferredLanguageEnum {
\\"it_IT\\" = \\"it_IT\\",

\\"en_GB\\" = \\"en_GB\\",

\\"es_ES\\" = \\"es_ES\\",

\\"de_DE\\" = \\"de_DE\\",

\\"fr_FR\\" = \\"fr_FR\\"
}

export type PreferredLanguage = t.TypeOf<typeof PreferredLanguage>;
export const PreferredLanguage = t.union(
[
enumType<PreferredLanguageEnum>(PreferredLanguageEnum, \\"PreferredLanguage\\"),
NonEmptyString
],
\\"ExtensiblePreferredLanguage\\"
);
"
`;

exports[`Openapi V3 |> gen-api-models should handle list of defintions 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
Expand Down Expand Up @@ -2362,6 +2434,7 @@ exports[`Openapi V3 |> gen-api-models should not contain a t.string but an enum:
import * as t from \\"io-ts\\";
import { Profile } from \\"./Profile\\";
import { enumType } from \\"@pagopa/ts-commons/lib/types\\";
import { NonEmptyString } from \\"@pagopa/ts-commons/lib/strings\\";

export enum StatusEnum {
\\"ACTIVATED\\" = \\"ACTIVATED\\"
Expand All @@ -2373,7 +2446,10 @@ export enum StatusEnum {

// required attributes
const AllOfWithXExtensibleEnum2R = t.interface({
status: enumType<StatusEnum>(StatusEnum, \\"status\\")
status: t.union(
[enumType<StatusEnum>(StatusEnum, \\"status\\"), NonEmptyString],
\\"Extensiblestatus\\"
)
});

// optional attributes
Expand Down
16 changes: 16 additions & 0 deletions src/commands/gen-api-models/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ describe.each`
expect(code).toMatchSnapshot("enum-simple");
});

it("should handle extensible enums", async () => {
const definitonName = "PreferredLanguage";
const definition = getDefinitionOrFail(spec, definitonName);

const code = await renderDefinitionCode(
definitonName,
getParser(spec).parseDefinition(
// @ts-ignore
definition
),
false
);

expect(code).toMatchSnapshot("extesible-enum");
});

it("should generate a record from additionalProperties", async () => {
const definitonName = "AdditionalPropsTest";
const definition = getDefinitionOrFail(spec, definitonName);
Expand Down
12 changes: 6 additions & 6 deletions src/commands/gen-api-models/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ describe.each`

expect(parsed).toEqual(
expect.objectContaining({
method: 'get',
operationId: 'testBinaryFileDownload',
responses: [ { e1: '200', e2: 'Buffer', e3: [] } ],
path: '/test-binary-file-download',
consumes: undefined,
method: "get",
operationId: "testBinaryFileDownload",
responses: [{ e1: "200", e2: "Buffer", e3: [] }],
path: "/test-binary-file-download",
consumes: undefined
})
);
});
Expand Down Expand Up @@ -297,7 +297,7 @@ describe.each`
definition
);

expect(parsed.enum).toEqual(expect.any(Array));
expect(parsed["x-extensible-enum"]).toEqual(expect.any(Array));
});

it("should handle AnObjectWithAnItemsField", async () => {
Expand Down
Loading