Skip to content

Commit

Permalink
Merge pull request #1468 from hey-api/fix/no-unchecked-indexed-access
Browse files Browse the repository at this point in the history
fix: handle indexed access checks
  • Loading branch information
mrlubos authored Dec 19, 2024
2 parents 6dfbd49 + 20d7497 commit ac442f0
Show file tree
Hide file tree
Showing 51 changed files with 138 additions and 107 deletions.
7 changes: 7 additions & 0 deletions .changeset/brown-parents-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hey-api/client-axios': patch
'@hey-api/client-fetch': patch
'@hey-api/openapi-ts': patch
---

fix: handle indexed access checks
1 change: 1 addition & 0 deletions packages/client-axios/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "ESNext",
"moduleResolution": "Bundler",
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"strict": true,
"target": "ES2022",
Expand Down
6 changes: 5 additions & 1 deletion packages/client-fetch/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ export const getParseAs = (
return;
}

const cleanContent = contentType.split(';')[0].trim();
const cleanContent = contentType.split(';')[0]?.trim();

if (!cleanContent) {
return;
}

if (
cleanContent.startsWith('application/json') ||
Expand Down
1 change: 1 addition & 0 deletions packages/client-fetch/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "ESNext",
"moduleResolution": "Bundler",
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"strict": true,
"target": "ES2022",
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/compiler/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const createSafeAccessExpression = (path: string[]) =>
createIdentifier({ text: element }),
);
},
createIdentifier({ text: path[0] }),
createIdentifier({ text: path[0]! }),
);

export const createAccessExpression = (path: string[]) =>
Expand All @@ -39,7 +39,7 @@ export const createAccessExpression = (path: string[]) =>
expression,
name: element,
}),
createIdentifier({ text: path[0] }),
createIdentifier({ text: path[0]! }),
);

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ export const toExpression = <T = unknown>({
// TODO; handle more than single nested level, i.e. foo.bar.baz
const parts = value.split('.');
return createPropertyAccessExpression({
expression: parts[0],
name: parts[1],
expression: parts[0]!,
name: parts[1]!,
});
}
return ots.string(value, unescape);
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function tsNodeToString({
*/
export function stringToTsNodes(value: string): ts.Node {
const file = createSourceFile(value);
return file.statements[0];
return file.statements[0]!;
}

export const createIdentifier = ({ text }: { text: string }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/generate/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('generateIndexFile', () => {

generateIndexFile({ files });

files.index.write();
files.index!.write();

expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining(path.resolve('index.ts')),
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/generate/indexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ export const generateIndexFile = ({ files }: { files: Files }): void => {
Object.keys(files)
.sort()
.forEach((name) => {
const file = files[name];
const file = files[name]!;

if (name === 'index' || file.isEmpty()) {
return;
}

if (['sdk', 'types'].includes(name)) {
files.index.add(
files.index!.add(
compiler.exportAllDeclaration({
module: `./${file.nameWithoutExtension()}`,
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export async function createClient(

Performance.end('createClient');

if (configs[0].logs.level === 'debug') {
if (configs[0]!.logs.level === 'debug') {
const perfReport = new PerformanceReport({
totalMark: 'createClient',
});
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/ir/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const operationPagination = ({
return {
in: 'body',
name: operation.body.pagination,
schema: finalSchema.properties![operation.body.pagination],
schema: finalSchema.properties![operation.body.pagination]!,
};
}

Expand Down
18 changes: 9 additions & 9 deletions packages/openapi-ts/src/ir/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const hasParameterGroupObjectRequired = (
parameterGroup?: Record<string, IR.ParameterObject>,
): boolean => {
for (const name in parameterGroup) {
if (parameterGroup[name].required) {
if (parameterGroup[name]!.required) {
return true;
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ export const parameterWithPagination = (
}

for (const name in parameters.cookie) {
const parameter = parameters.cookie[name];
const parameter = parameters.cookie[name]!;
if (parameter.pagination) {
return {
in: parameter.location,
Expand All @@ -58,13 +58,13 @@ export const parameterWithPagination = (
schema:
parameter.pagination === true
? parameter.schema
: parameter.schema.properties![parameter.pagination],
: parameter.schema.properties![parameter.pagination]!,
};
}
}

for (const name in parameters.header) {
const parameter = parameters.header[name];
const parameter = parameters.header[name]!;
if (parameter.pagination) {
return {
in: parameter.location,
Expand All @@ -75,13 +75,13 @@ export const parameterWithPagination = (
schema:
parameter.pagination === true
? parameter.schema
: parameter.schema.properties![parameter.pagination],
: parameter.schema.properties![parameter.pagination]!,
};
}
}

for (const name in parameters.path) {
const parameter = parameters.path[name];
const parameter = parameters.path[name]!;
if (parameter.pagination) {
return {
in: parameter.location,
Expand All @@ -92,13 +92,13 @@ export const parameterWithPagination = (
schema:
parameter.pagination === true
? parameter.schema
: parameter.schema.properties![parameter.pagination],
: parameter.schema.properties![parameter.pagination]!,
};
}
}

for (const name in parameters.query) {
const parameter = parameters.query[name];
const parameter = parameters.query[name]!;
if (parameter.pagination) {
return {
in: parameter.location,
Expand All @@ -109,7 +109,7 @@ export const parameterWithPagination = (
schema:
parameter.pagination === true
? parameter.schema
: parameter.schema.properties![parameter.pagination],
: parameter.schema.properties![parameter.pagination]!,
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-ts/src/ir/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ export const parseIR = async ({ context }: { context: IR.Context }) => {

if (context.ir.components) {
for (const name in context.ir.components.schemas) {
const schema = context.ir.components.schemas[name];
const schema = context.ir.components.schemas[name]!;
const $ref = `#/components/schemas/${name}`;
await context.broadcast('schema', { $ref, name, schema });
}

for (const name in context.ir.components.parameters) {
const parameter = context.ir.components.parameters[name];
const parameter = context.ir.components.parameters[name]!;
const $ref = `#/components/parameters/${name}`;
await context.broadcast('parameter', { $ref, name, parameter });
}

for (const name in context.ir.components.requestBodies) {
const requestBody = context.ir.components.requestBodies[name];
const requestBody = context.ir.components.requestBodies[name]!;
const $ref = `#/components/requestBodies/${name}`;
await context.broadcast('requestBody', { $ref, name, requestBody });
}
Expand Down
11 changes: 6 additions & 5 deletions packages/openapi-ts/src/openApi/3.0.x/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export const parseV3_0_X = (context: IR.Context<OpenApiV3_0_X>) => {
// TODO: parser - handle more component types, old parser handles only parameters and schemas
if (context.spec.components) {
for (const name in context.spec.components.securitySchemes) {
const securityOrReference = context.spec.components.securitySchemes[name];
const securityOrReference =
context.spec.components.securitySchemes[name]!;
const securitySchemeObject =
'$ref' in securityOrReference
? context.resolveRef<SecuritySchemeObject>(securityOrReference.$ref)
Expand All @@ -52,7 +53,7 @@ export const parseV3_0_X = (context: IR.Context<OpenApiV3_0_X>) => {
continue;
}

const parameterOrReference = context.spec.components.parameters[name];
const parameterOrReference = context.spec.components.parameters[name]!;
const parameter =
'$ref' in parameterOrReference
? context.resolveRef<ParameterObject>(parameterOrReference.$ref)
Expand All @@ -72,7 +73,7 @@ export const parseV3_0_X = (context: IR.Context<OpenApiV3_0_X>) => {
}

const requestBodyOrReference =
context.spec.components.requestBodies[name];
context.spec.components.requestBodies[name]!;
const requestBody =
'$ref' in requestBodyOrReference
? context.resolveRef<RequestBodyObject>(requestBodyOrReference.$ref)
Expand All @@ -91,7 +92,7 @@ export const parseV3_0_X = (context: IR.Context<OpenApiV3_0_X>) => {
continue;
}

const schema = context.spec.components.schemas[name];
const schema = context.spec.components.schemas[name]!;

parseSchema({
$ref,
Expand All @@ -102,7 +103,7 @@ export const parseV3_0_X = (context: IR.Context<OpenApiV3_0_X>) => {
}

for (const path in context.spec.paths) {
const pathItem = context.spec.paths[path as keyof PathsObject];
const pathItem = context.spec.paths[path as keyof PathsObject]!;

const finalPathItem = pathItem.$ref
? {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/3.0.x/parser/mediaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const mediaTypeObject = ({
for (const mediaType in content) {
return {
mediaType,
schema: content[mediaType].schema,
schema: content[mediaType]!.schema,
type: mediaTypeToIrMediaType({ mediaType }),
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/3.0.x/parser/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const paginationField = ({
paginationKeywordsRegExp.lastIndex = 0;

if (paginationKeywordsRegExp.test(name)) {
const property = schema.properties[name];
const property = schema.properties[name]!;

if (typeof property !== 'boolean' && !('$ref' in property)) {
const schemaType = getSchemaType({ schema: property });
Expand Down
10 changes: 5 additions & 5 deletions packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const parseObject = ({
const schemaProperties: Record<string, IR.SchemaObject> = {};

for (const name in schema.properties) {
const property = schema.properties[name];
const property = schema.properties[name]!;
if (typeof property === 'boolean') {
// TODO: parser - handle boolean properties
} else {
Expand Down Expand Up @@ -395,14 +395,14 @@ const parseAllOf = ({

// TODO: parser - this is a hack to bring back up meta fields
// without it, some schemas were missing original deprecated
if (nestedItems[0].deprecated) {
irSchema.deprecated = nestedItems[0].deprecated;
if (nestedItems[0]!.deprecated) {
irSchema.deprecated = nestedItems[0]!.deprecated;
}

// TODO: parser - this is a hack to bring back up meta fields
// without it, some schemas were missing original description
if (nestedItems[0].description) {
irSchema.description = nestedItems[0].description;
if (nestedItems[0]!.description) {
irSchema.description = nestedItems[0]!.description;
}
}

Expand Down
11 changes: 6 additions & 5 deletions packages/openapi-ts/src/openApi/3.1.x/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
// TODO: parser - handle more component types, old parser handles only parameters and schemas
if (context.spec.components) {
for (const name in context.spec.components.securitySchemes) {
const securityOrReference = context.spec.components.securitySchemes[name];
const securityOrReference =
context.spec.components.securitySchemes[name]!;
const securitySchemeObject =
'$ref' in securityOrReference
? context.resolveRef<SecuritySchemeObject>(securityOrReference.$ref)
Expand All @@ -52,7 +53,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
continue;
}

const parameterOrReference = context.spec.components.parameters[name];
const parameterOrReference = context.spec.components.parameters[name]!;
const parameter =
'$ref' in parameterOrReference
? context.resolveRef<ParameterObject>(parameterOrReference.$ref)
Expand All @@ -72,7 +73,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
}

const requestBodyOrReference =
context.spec.components.requestBodies[name];
context.spec.components.requestBodies[name]!;
const requestBody =
'$ref' in requestBodyOrReference
? context.resolveRef<RequestBodyObject>(requestBodyOrReference.$ref)
Expand All @@ -91,7 +92,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
continue;
}

const schema = context.spec.components.schemas[name];
const schema = context.spec.components.schemas[name]!;

parseSchema({
$ref,
Expand All @@ -102,7 +103,7 @@ export const parseV3_1_X = (context: IR.Context<OpenApiV3_1_X>) => {
}

for (const path in context.spec.paths) {
const pathItem = context.spec.paths[path as keyof PathsObject];
const pathItem = context.spec.paths[path as keyof PathsObject]!;

const finalPathItem = pathItem.$ref
? {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/3.1.x/parser/mediaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const mediaTypeObject = ({
for (const mediaType in content) {
return {
mediaType,
schema: content[mediaType].schema,
schema: content[mediaType]!.schema,
type: mediaTypeToIrMediaType({ mediaType }),
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/openApi/3.1.x/parser/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const paginationField = ({
paginationKeywordsRegExp.lastIndex = 0;

if (paginationKeywordsRegExp.test(name)) {
const property = schema.properties[name];
const property = schema.properties[name]!;

if (typeof property !== 'boolean') {
const schemaTypes = getSchemaTypes({ schema: property });
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ const parseObject = ({
const schemaProperties: Record<string, IR.SchemaObject> = {};

for (const name in schema.properties) {
const property = schema.properties[name];
const property = schema.properties[name]!;
if (typeof property === 'boolean') {
// TODO: parser - handle boolean properties
} else {
Expand Down Expand Up @@ -820,7 +820,7 @@ const parseType = ({
irSchema,
schema: {
...schema,
type: schemaTypes[0],
type: schemaTypes[0]!,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getDefault = (
model?.export === 'enum' &&
model.enum?.[definition.default as number]
) {
const { value } = model.enum[definition.default as number];
const { value } = model.enum[definition.default as number]!;
return value;
}
return definition.default;
Expand Down
Loading

0 comments on commit ac442f0

Please sign in to comment.