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

fix: support additional properties without xDictionaryKey #52

Merged

Conversation

RicherStAmand
Copy link
Contributor

@RicherStAmand RicherStAmand commented Apr 18, 2024

SamplesMarketplace swagger have this value :
https://api3-dev.landr.com/samplesmarketplace/swagger/v1/swagger.json#/

 "FilterCounters": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "sampleTypeFilters": {
            "type": "object",
            "nullable": true,
            "x-dictionaryKey": {
              "$ref": "#/components/schemas/SampleType"
            },
            "additionalProperties": {
              "type": "integer",
              "format": "int64"
            }
          },
          "instrumentFilters": {
            "type": "object",
            "nullable": true,
            "additionalProperties": {
              "type": "integer",
              "format": "int64"
            }
          },
          "genreFilters": {
            "type": "object",
            "nullable": true,
            "additionalProperties": {
              "type": "integer",
              "format": "int64"
            }
          },
          "sfxFilters": {
            "type": "object",
            "nullable": true,
            "additionalProperties": {
              "type": "integer",
              "format": "int64"
            }
          },
          "tagFilters": {
            "type": "object",
            "nullable": true,
            "additionalProperties": {
              "type": "integer",
              "format": "int64"
            }
          }
        }
      },

that got type as is :

export interface FilterCounters {
    sampleTypeFilters: {
        [key in SampleType]: number;
    };
}

where I was expecting this :

export interface FilterCounters {
    sampleTypeFilters: {
        [key in SampleType]: number;
    },
    instrumentFilters: {[key: string]: number;},
    genreFilters: {[key: string]: number;},
    sfxFilters: {[key: string]: number;},
    tagFilters: {[key: string]: number;}
}

I found that we were not properly handling value with additionalProperties but without a xDictionaryKey.

Part 1 - This PR
Part 2 - #53
Part 3 - #54

if (additionalProperties.items && additionalProperties.items[SwaggerProps.$ref]) {
res = `${parseRefType(additionalProperties.items[SwaggerProps.$ref].split('/'))}[]`;
} else {
res = `"// TODO: Something is wrong"`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldnt recommend to put TODOs in code even if its for logging purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but in this PR I focused on adding the support for additional properties type without dictionnary, without changing existing patterns. - This part of the code is in fact duplicated from the one that handle the additionalProperties that have dictionnary ref.

I have upcoming PRs to improve on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at the end of the day, we will still want to write in the result file.
Something more like this ?
// Error: DataTypes.Array without items or items ref is not supported

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Its more helpful

Copy link
Contributor

@ardeois ardeois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
@nemrosim can you check as well, as you are the most familiar with this repo

Copy link
Collaborator

@nemrosim nemrosim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, few comments

Comment on lines 14 to 22
describe(`getDescription`, () => {
scenarios.forEach(config => {
it(`should return ${config.expected} when props is ${config.props}`, async () => {
const result = getDescription(config.props);

expect(result).toBe(config.expected);
});
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use it.each instead of scenarios. And why test is async?

it.each([['case 1',{input: output}],['case 2', {input, output}]])('%s ....', (_, props) => <do something>);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 80 to 88
describe(`getResultStringForAdditionalPropertiesType`, () => {
scenarios.forEach(config => {
it(`should return expected result when props is ${config.scenarioName}`, async () => {
const result = getResultStringForAdditionalPropertiesType(config.props);

expect(result).toBe(config.expected);
});
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, async not needed too, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

description,
propertyName,
}: {
additionalProperties: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why any? I see that you are testing additionalProperties with known props. (I understand that probably there could be some changes in the future, but at least we can define known props at the moment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additionalProperties is returned by getSchemaProperties, which type it as additionalProperties: any
We don't have any type guard or logic in typesConverter to known the types of additionalProperties by the time we pass it to getResultStringForAdditionalPropertiesType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course, that could be improve, but in this PR I wanted to have a minimal number of change, just introducing the new helpers needed to resolve the issue I add with the samples marketplace types.

@RicherStAmand RicherStAmand added bug Something isn't working Part 1 and removed bug Something isn't working labels Apr 23, 2024
* fix: move helpers in individual files, improve typing

* fix: replace 4 space by a tab characters, update unit test

* fix: helpers for undefined type, ref and oneOf

* fix: typing

* fix: remove change that impact snapshot or expected result of unit test

* fix: adjust props

* fix: adding coverage (#54)

* fix: add coverage

* fix: update coverage

* fix: improve error message

* fix: improve error message

* fix: convert space into tab charaters and use return character

* fix: commit yarn.lock

* fix: update test

* fix: remove describe, remove test name
* fix: generate mock for additional properties without dictionnary

* fix: update error message

* fix: update mock generator and coverage
@RicherStAmand RicherStAmand merged commit c188273 into master Apr 24, 2024
1 check passed
@RicherStAmand RicherStAmand deleted the fix/SP-10428-generate-type-for-object-without-dictionnary branch April 24, 2024 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants