Skip to content

Commit

Permalink
[ICC-135] Replaced t.dictionary with t.record (#292)
Browse files Browse the repository at this point in the history
* Replaced t.dictionary with t.record
* added some IT for t.record
  • Loading branch information
Garma00 authored Jul 15, 2022
1 parent 48ef17f commit 916a063
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
24 changes: 22 additions & 2 deletions e2e/src/__tests__/test-api/definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import { EnumFalseTest } from "../../generated/testapi/EnumFalseTest";
import { EnumTrueTest } from "../../generated/testapi/EnumTrueTest";
import { AllOfWithOneElementTest } from "../../generated/testapi/AllOfWithOneElementTest";
import { AllOfWithOneRefElementTest } from "../../generated/testapi/AllOfWithOneRefElementTest";
import { AdditionalPropsTest } from "../../generated/testapi/AdditionalPropsTest";


import * as E from "fp-ts/lib/Either"

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

// if there's no need for this suite in this particular run, just skip it

Expand Down Expand Up @@ -334,12 +335,31 @@ describe("AllOfWithOneElementTest definition", () => {
expect(E.isRight(AllOfWithOneElementTest.decode(okElement))).toBeTruthy();
})

it("Should return a left aaa", () => {
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"]};

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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Openapi V2 |> gen-api-models should generate a dictionary from additionalProperties: additional-properties 1`] = `
exports[`Openapi V2 |> gen-api-models should generate a record from additionalProperties: additional-properties 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
* See https:/pagopa/io-utils
Expand All @@ -11,7 +11,7 @@ import * as t from \\"io-ts\\";

// additional attributes

export const AdditionalPropsTest = t.dictionary(
export const AdditionalPropsTest = t.record(
t.string,

t.readonlyArray(t.number, \\"array of number\\"),
Expand All @@ -23,7 +23,7 @@ export type AdditionalPropsTest = t.TypeOf<typeof AdditionalPropsTest>;
"
`;

exports[`Openapi V2 |> gen-api-models should generate a dictionary from additionalProperties: true: additional-properties-true 1`] = `
exports[`Openapi V2 |> gen-api-models should generate a record from additionalProperties: true: additional-properties-true 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
* See https:/pagopa/io-utils
Expand All @@ -34,7 +34,7 @@ import * as t from \\"io-ts\\";

// additional attributes

export const AdditionalPropsTrueTest = t.dictionary(
export const AdditionalPropsTrueTest = t.record(
t.string,
t.any,
\\"AdditionalPropsTrueTest\\"
Expand Down Expand Up @@ -1568,7 +1568,7 @@ import * as t from \\"io-ts\\";
import { withDefault } from \\"@pagopa/ts-commons/lib/types\\";

type AdditionalpropsDefaultBase = t.TypeOf<typeof AdditionalpropsDefaultBase>;
const AdditionalpropsDefaultBase = t.dictionary(
const AdditionalpropsDefaultBase = t.record(
t.string,

t.readonlyArray(t.number, \\"array of number\\"),
Expand Down Expand Up @@ -1728,7 +1728,7 @@ exports[`Openapi V2 |> gen-api-models should support multiple success cases 1`]
export const testMultipleSuccessDefaultDecoder = () => testMultipleSuccessDecoder();"
`;

exports[`Openapi V3 |> gen-api-models should generate a dictionary from additionalProperties: additional-properties 1`] = `
exports[`Openapi V3 |> gen-api-models should generate a record from additionalProperties: additional-properties 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
* See https:/pagopa/io-utils
Expand All @@ -1739,7 +1739,7 @@ import * as t from \\"io-ts\\";

// additional attributes

export const AdditionalPropsTest = t.dictionary(
export const AdditionalPropsTest = t.record(
t.string,

t.readonlyArray(t.number, \\"array of number\\"),
Expand All @@ -1751,7 +1751,7 @@ export type AdditionalPropsTest = t.TypeOf<typeof AdditionalPropsTest>;
"
`;

exports[`Openapi V3 |> gen-api-models should generate a dictionary from additionalProperties: true: additional-properties-true 1`] = `
exports[`Openapi V3 |> gen-api-models should generate a record from additionalProperties: true: additional-properties-true 1`] = `
"/**
* Do not edit this file it is auto-generated by io-utils / gen-api-models.
* See https:/pagopa/io-utils
Expand All @@ -1762,7 +1762,7 @@ import * as t from \\"io-ts\\";

// additional attributes

export const AdditionalPropsTrueTest = t.dictionary(
export const AdditionalPropsTrueTest = t.record(
t.string,
t.any,
\\"AdditionalPropsTrueTest\\"
Expand Down Expand Up @@ -3267,7 +3267,7 @@ import * as t from \\"io-ts\\";
import { withDefault } from \\"@pagopa/ts-commons/lib/types\\";

type AdditionalpropsDefaultBase = t.TypeOf<typeof AdditionalpropsDefaultBase>;
const AdditionalpropsDefaultBase = t.dictionary(
const AdditionalpropsDefaultBase = t.record(
t.string,

t.readonlyArray(t.number, \\"array of number\\"),
Expand Down
13 changes: 8 additions & 5 deletions src/commands/gen-api-models/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe.each`
expect(code).toMatchSnapshot("enum-simple");
});

it("should generate a dictionary from additionalProperties", async () => {
it("should generate a record from additionalProperties", async () => {
const definitonName = "AdditionalPropsTest";
const definition = getDefinitionOrFail(spec, definitonName);

Expand All @@ -207,11 +207,12 @@ describe.each`
false
);

expect(code).toContain("t.dictionary");
expect(code).toContain("t.record");
expect(code).not.toContain("t.dictionary");
expect(code).toMatchSnapshot("additional-properties");
});

it("should generate a dictionary from additionalProperties: true", async () => {
it("should generate a record from additionalProperties: true", async () => {
const definitonName = "AdditionalPropsTrueTest";
const definition = getDefinitionOrFail(spec, definitonName);

Expand All @@ -224,7 +225,8 @@ describe.each`
false
);

expect(code).toContain("t.dictionary");
expect(code).toContain("t.record");
expect(code).not.toContain("t.dictionary");
expect(code).toContain("t.any");
expect(code).toMatchSnapshot("additional-properties-true");
});
Expand All @@ -242,7 +244,8 @@ describe.each`
false
);

expect(code).toContain("t.dictionary");
expect(code).toContain("t.record");
expect(code).not.toContain("t.dictionary");
expect(code).toContain("withDefault");
expect(code).toMatchSnapshot("additional-properties-default");
});
Expand Down
4 changes: 2 additions & 2 deletions templates/macros.njk
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@
{% if definition.additionalProperties %}
// additional attributes
{% if definition.additionalProperties == true -%}
{% set typedef %}t.dictionary(t.string, t.any, "{{ definitionName }}"){% endset %}
{% set typedef %}t.record(t.string, t.any, "{{ definitionName }}"){% endset %}
{% else %}
{% set typedef %}t.dictionary(t.string,
{% set typedef %}t.record(t.string,
{{ defineObjectProperty(false, definition.additionalProperties, camelCasedPropNames) }} "{{ definitionName }}")
{% endset %}
{%- endif %}
Expand Down

0 comments on commit 916a063

Please sign in to comment.