Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROPOSAL] x-extensible-enum generate real extensible type definition #295

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 54 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,16 @@ 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/testapiV3/PreferredLanguage";
import { PreferredLanguage as ExtensiblePreferredLanguage } from "../../generated/testapiV3-unstrict/PreferredLanguage";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";

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

Expand Down Expand Up @@ -324,24 +330,56 @@ 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 = ExtensiblePreferredLanguage.decode(l1Ok);
const strictResult = PreferredLanguage.decode(l1Ok);
expect(E.isRight(result)).toBe(true);
expect(E.isRight(strictResult)).toBe(true);
});

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

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

it("should fail decode extensible enum with empty string", () => {
const result = ExtensiblePreferredLanguage.decode(l4Ko);
const strictResult = PreferredLanguage.decode(l4Ko);
expect(E.isLeft(result)).toBe(true);
expect(E.isLeft(strictResult)).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 +389,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
82 changes: 59 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,13 @@ 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 { PreferredLanguage as ExtensiblePreferredLanguage } from "../../generated/testapi-unstrict/PreferredLanguage";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";

const { generatedFilesDir } = config.specs.testapi;

Expand Down Expand Up @@ -326,43 +331,73 @@ 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 = ExtensiblePreferredLanguage.decode(l1Ok);
const strictResult = PreferredLanguage.decode(l1Ok);
expect(E.isRight(result)).toBe(true);
expect(E.isRight(strictResult)).toBe(true);
});

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

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

it("should fail decode extensible enum with empty string", () => {
const result = ExtensiblePreferredLanguage.decode(l4Ko);
const strictResult = PreferredLanguage.decode(l4Ko);
expect(E.isLeft(result)).toBe(true);
expect(E.isLeft(strictResult)).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 +407,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
21 changes: 19 additions & 2 deletions e2e/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,35 @@ export default {
generatedFilesDir: `${GENERATED_BASE_DIR}/be`,
isSpecEnabled: includeInList(process.env.INCLUDE_SPECS, "be"),
mockPort: 4102,
strictInterfaces: true,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/be.yaml`
},
testapi: {
strictTestapi: {
generatedFilesDir: `${GENERATED_BASE_DIR}/testapi`,
isSpecEnabled: includeInList(process.env.INCLUDE_SPECS, "testapi"),
mockPort: 4101,
strictInterfaces: true,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/api.yaml`
},
testapiV3: {
strictTestapiV3: {
generatedFilesDir: `${GENERATED_BASE_DIR}/testapiV3`,
isSpecEnabled: includeInList(process.env.INCLUDE_SPECS, "testapiV3"),
mockPort: 4103,
strictInterfaces: true,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/openapi_v3/api.yaml`
},
testapi: {
generatedFilesDir: `${GENERATED_BASE_DIR}/testapi-unstrict`,
isSpecEnabled: includeInList(process.env.INCLUDE_SPECS, "testapi"),
mockPort: 4104,
strictInterfaces: false,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/api.yaml`
},
testapiV3: {
generatedFilesDir: `${GENERATED_BASE_DIR}/testapiV3-unstrict`,
isSpecEnabled: includeInList(process.env.INCLUDE_SPECS, "testapiV3"),
mockPort: 4105,
strictInterfaces: false,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/openapi_v3/api.yaml`
}
}
Expand Down
4 changes: 2 additions & 2 deletions e2e/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async () => {
const { specs, skipClient, skipGeneration } = config;
const tasks = Object.values(specs)
.filter(({ isSpecEnabled }) => isSpecEnabled)
.map(({ url, mockPort, generatedFilesDir }) => {
.map(({ url, mockPort, generatedFilesDir, strictInterfaces }) => {
// eslint-disable-next-line sonarjs/prefer-immediate-return
const p = pipe(
skipGeneration
Expand All @@ -58,7 +58,7 @@ export default async () => {
definitionsDirPath: generatedFilesDir,
generateClient: true,
specFilePath: url,
strictInterfaces: true
strictInterfaces
}),
TE.chain(() => (skipClient ? noopTE : tsStartServer(url, mockPort)))
);
Expand Down
Loading