Skip to content

Commit b4a69f6

Browse files
committed
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 <contact@emersion.fr>
1 parent 614a329 commit b4a69f6

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

front/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
"i18next-fs-backend": "^2.3.1",
141141
"json-key-path-list": "^2.0.3",
142142
"license-checker-rseidelsohn": "^4.4.2",
143+
"openapi-types": "^12.1.3",
143144
"prettier": "^3.2.5",
144145
"sass": "^1.70.0",
145146
"ts-node": "^10.9.2",

front/scripts/i18n-api-errors.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SwaggerParser from '@apidevtools/swagger-parser';
22
import i18next from 'i18next';
33
import { noop } from 'lodash';
4+
import type { OpenAPI } from 'openapi-types';
45

56
// relative the project's root
67
const openapi_path = '../editoast/openapi.yaml';
@@ -65,19 +66,21 @@ async function checkI18N(error: any): Promise<string[]> {
6566

6667
async function run() {
6768
try {
68-
const api = await SwaggerParser.validate(openapi_path);
69+
const api: OpenAPI.Document = await SwaggerParser.validate(openapi_path);
70+
71+
if (!('openapi' in api)) throw new Error('Expected an OpenAPI schema');
6972

7073
// Do some checks on the generic error
71-
if (!api.components.schemas.EditoastError)
72-
throw new Error(`"EditoastError" can't be found in "components > schemas"`);
73-
if (!api.components.schemas.EditoastError.oneOf)
74+
const editoastError = api?.components?.schemas?.EditoastError;
75+
if (!editoastError) throw new Error(`"EditoastError" can't be found in "components > schemas"`);
76+
if (!('oneOf' in editoastError) || !editoastError.oneOf)
7477
throw new Error(`Expected "EditoastError" to be a "oneOf" object`);
7578

7679
// Check i18n for all errors
7780
const errors = (
7881
await Promise.all(
7982
// eslint-disable-next-line @typescript-eslint/no-explicit-any
80-
api.components.schemas.EditoastError.oneOf.map((error: any) => checkI18N(error))
83+
editoastError.oneOf.map((error: any) => checkI18N(error))
8184
)
8285
).flat();
8386

front/yarn.lock

+33-3
Original file line numberDiff line numberDiff line change
@@ -8079,6 +8079,11 @@ once@^1.3.0, once@^1.3.1, once@^1.4.0:
80798079
dependencies:
80808080
wrappy "1"
80818081

8082+
openapi-types@^12.1.3:
8083+
version "12.1.3"
8084+
resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3"
8085+
integrity sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw==
8086+
80828087
openapi-typescript-codegen@^0.27.0:
80838088
version "0.27.0"
80848089
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:
97339738
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
97349739
integrity sha512-R3f198pcvnB+5IpnBlRkphuE9n46WyVl8I39W/ZUTZLz4nqSP/oLYUrcnJrw462Ds8he4YKMov2efsTIw1BDGQ==
97359740

9736-
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
9741+
"string-width-cjs@npm:string-width@^4.2.0":
9742+
version "4.2.3"
9743+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
9744+
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
9745+
dependencies:
9746+
emoji-regex "^8.0.0"
9747+
is-fullwidth-code-point "^3.0.0"
9748+
strip-ansi "^6.0.1"
9749+
9750+
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
97379751
version "4.2.3"
97389752
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
97399753
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -9835,7 +9849,14 @@ stringify-entities@^4.0.0:
98359849
character-entities-html4 "^2.0.0"
98369850
character-entities-legacy "^3.0.0"
98379851

9838-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
9852+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
9853+
version "6.0.1"
9854+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
9855+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
9856+
dependencies:
9857+
ansi-regex "^5.0.1"
9858+
9859+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
98399860
version "6.0.1"
98409861
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
98419862
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -11041,7 +11062,16 @@ wordwrap@^1.0.0:
1104111062
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
1104211063
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==
1104311064

11044-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
11065+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
11066+
version "7.0.0"
11067+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
11068+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
11069+
dependencies:
11070+
ansi-styles "^4.0.0"
11071+
string-width "^4.1.0"
11072+
strip-ansi "^6.0.0"
11073+
11074+
wrap-ansi@^7.0.0:
1104511075
version "7.0.0"
1104611076
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1104711077
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==

0 commit comments

Comments
 (0)