From c6ce0d5dc7672e8d0e38288871a69fcfc511098f Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 19 Nov 2024 15:01:45 +0100 Subject: [PATCH] front: fix missing openapi-types in i18n-api-errors script The openapi-types package is a peerDependency of @apidevtools/swagger-parser. yarn doesn't install peerDependencies by default. This is an issue because when openapi-types is missing, TypeScript doesn't have any typing information about the values returned by SwaggerParser.validate(), and assumes `any`. This results in type checking getting completely disabled for code manipulating the return value. Indeed, after installing openapi-types, `yarn build` indicates TypeScript errors in i18n-api-errors.ts. Fix these errors by checking that the returned schema is OpenAPI (as opposed to Swagger), and that `EditoastError` is a schema object. Signed-off-by: Simon Ser --- front/package.json | 1 + front/scripts/i18n-api-errors.ts | 15 +++++++------ front/yarn.lock | 36 +++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/front/package.json b/front/package.json index ba5624ce986..2d18b671949 100644 --- a/front/package.json +++ b/front/package.json @@ -140,6 +140,7 @@ "i18next-fs-backend": "^2.3.1", "json-key-path-list": "^2.0.3", "license-checker-rseidelsohn": "^4.4.2", + "openapi-types": "^12.1.3", "prettier": "^3.2.5", "sass": "^1.70.0", "ts-node": "^10.9.2", diff --git a/front/scripts/i18n-api-errors.ts b/front/scripts/i18n-api-errors.ts index 66ae61bc526..e8e7b93630a 100644 --- a/front/scripts/i18n-api-errors.ts +++ b/front/scripts/i18n-api-errors.ts @@ -1,6 +1,7 @@ import SwaggerParser from '@apidevtools/swagger-parser'; import i18next from 'i18next'; import { noop } from 'lodash'; +import type { OpenAPI } from 'openapi-types'; // relative the project's root const openapi_path = '../editoast/openapi.yaml'; @@ -65,19 +66,21 @@ async function checkI18N(error: any): Promise { async function run() { try { - const api = await SwaggerParser.validate(openapi_path); + const api: OpenAPI.Document = await SwaggerParser.validate(openapi_path); + + if (!('openapi' in api)) throw new Error('Expected an OpenAPI schema'); // Do some checks on the generic error - if (!api.components.schemas.EditoastError) - throw new Error(`"EditoastError" can't be found in "components > schemas"`); - if (!api.components.schemas.EditoastError.oneOf) - throw new Error(`Expected "EditoastError" to be a "oneOf" object`); + const EditoastError = api?.components?.schemas?.EditoastError; + if (!EditoastError) throw new Error(`"EditoastError" can't be found in "components > schemas"`); + if (!('oneOf' in EditoastError)) throw new Error(`"EditoastError" is not a schema object`); + if (!EditoastError.oneOf) throw new Error(`Expected "EditoastError" to be a "oneOf" object`); // Check i18n for all errors const errors = ( await Promise.all( // eslint-disable-next-line @typescript-eslint/no-explicit-any - api.components.schemas.EditoastError.oneOf.map((error: any) => checkI18N(error)) + EditoastError.oneOf.map((error: any) => checkI18N(error)) ) ).flat(); diff --git a/front/yarn.lock b/front/yarn.lock index 740f73e7367..cbbb7cda137 100644 --- a/front/yarn.lock +++ b/front/yarn.lock @@ -8079,6 +8079,11 @@ once@^1.3.0, once@^1.3.1, once@^1.4.0: dependencies: wrappy "1" +openapi-types@^12.1.3: + version "12.1.3" + resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3" + integrity sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw== + openapi-typescript-codegen@^0.27.0: version "0.27.0" resolved "https://registry.yarnpkg.com/openapi-typescript-codegen/-/openapi-typescript-codegen-0.27.0.tgz#832ddb4435391ec2c0406a8ed27d73dee36616e9" @@ -9733,7 +9738,16 @@ strict-uri-encode@^1.0.0: resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713" integrity sha512-R3f198pcvnB+5IpnBlRkphuE9n46WyVl8I39W/ZUTZLz4nqSP/oLYUrcnJrw462Ds8he4YKMov2efsTIw1BDGQ== -"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0": + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + +string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -9835,7 +9849,14 @@ stringify-entities@^4.0.0: character-entities-html4 "^2.0.0" character-entities-legacy "^3.0.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + +strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -11041,7 +11062,16 @@ wordwrap@^1.0.0: resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb" integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + +wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==