Skip to content

Commit

Permalink
front: fix missing openapi-types in i18n-api-errors script
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
emersion committed Nov 19, 2024
1 parent 614a329 commit b4a69f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions front/scripts/i18n-api-errors.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -65,19 +66,21 @@ async function checkI18N(error: any): Promise<string[]> {

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)
const editoastError = api?.components?.schemas?.EditoastError;
if (!editoastError) throw new Error(`"EditoastError" can't be found in "components > schemas"`);
if (!('oneOf' in editoastError) || !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();

Expand Down
36 changes: 33 additions & 3 deletions front/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit b4a69f6

Please sign in to comment.