Skip to content

Commit

Permalink
update lint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Aug 1, 2024
1 parent c9407a1 commit fd80d54
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 57 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80
"printWidth": 80,
"endOfLine": "auto"
}
2 changes: 1 addition & 1 deletion src/generators/csharp/CSharpConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function getFullTypeDefinition(
typeName: string,
partOfProperty: ConstrainedObjectPropertyModel | undefined
) {
return partOfProperty?.required ?? true ? typeName : `${typeName}?`;
return (partOfProperty?.required ?? true) ? typeName : `${typeName}?`;
}

const fromEnumValueToType = (
Expand Down
11 changes: 9 additions & 2 deletions src/models/AsyncapiV2Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export class AsyncapiV2Schema {
'Could not convert input to expected copy of AsyncapiV2Schema'
);
}

// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, AsyncapiV2Schema> = new Map()
Expand Down Expand Up @@ -124,7 +126,9 @@ export class AsyncapiV2Schema {
const schema = new AsyncapiV2Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
if (prop === undefined) {
continue;
}
let copyProp: any = prop;

// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
Expand All @@ -148,7 +152,10 @@ export class AsyncapiV2Schema {
// Special cases are properties that should be a basic object
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = AsyncapiV2Schema.internalToSchema(prop2, seenSchemas);
copyProp[String(propName2)] = AsyncapiV2Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = AsyncapiV2Schema.internalToSchema(prop, seenSchemas);
Expand Down
16 changes: 10 additions & 6 deletions src/models/CommonModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class CommonModel {
}
throw new Error('Could not convert input to expected copy of CommonModel');
}

// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, CommonModel> = new Map()
Expand Down Expand Up @@ -73,17 +75,19 @@ export class CommonModel {
const schema = new CommonModel();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
if (prop === undefined) {
continue;
}
let copyProp: any = prop;
// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
if (propName !== 'originalInput' && propName !== 'enum') {
if (
propName === 'properties' ||
propName === 'patternProperties'
) {
if (propName === 'properties' || propName === 'patternProperties') {
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = CommonModel.internalToSchema(prop2, seenSchemas);
copyProp[String(propName2)] = CommonModel.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = CommonModel.internalToSchema(prop, seenSchemas);
Expand Down
10 changes: 8 additions & 2 deletions src/models/Draft4Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class Draft4Schema {
}
throw new Error('Could not convert input to expected copy of Draft4Schema');
}
// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, Draft4Schema> = new Map()
Expand Down Expand Up @@ -78,7 +79,9 @@ export class Draft4Schema {
const schema = new Draft4Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
if (prop === undefined) {
continue;
}
let copyProp: any = prop;

// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
Expand All @@ -92,7 +95,10 @@ export class Draft4Schema {
) {
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = Draft4Schema.internalToSchema(prop2, seenSchemas);
copyProp[String(propName2)] = Draft4Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = Draft4Schema.internalToSchema(prop, seenSchemas);
Expand Down
10 changes: 8 additions & 2 deletions src/models/Draft6Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Draft6Schema {
}
throw new Error('Could not convert input to expected copy of Draft6Schema');
}
// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, Draft6Schema> = new Map()
Expand Down Expand Up @@ -84,7 +85,9 @@ export class Draft6Schema {
const schema = new Draft6Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
if (prop === undefined) {
continue;
}
let copyProp: any = prop;

// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
Expand All @@ -103,7 +106,10 @@ export class Draft6Schema {
) {
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = Draft6Schema.internalToSchema(prop2, seenSchemas);
copyProp[String(propName2)] = Draft6Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = Draft6Schema.internalToSchema(prop, seenSchemas);
Expand Down
51 changes: 29 additions & 22 deletions src/models/Draft7Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class Draft7Schema {
}
throw new Error('Could not convert input to expected copy of Draft7Schema');
}

// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, Draft7Schema> = new Map()
Expand Down Expand Up @@ -94,31 +96,36 @@ export class Draft7Schema {
const schema = new Draft7Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
let copyProp: any = prop;
// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
if (prop === undefined) {
continue;
}
let copyProp: any = prop;
// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
if (
propName !== 'default' &&
propName !== 'examples' &&
propName !== 'const' &&
propName !== 'enum'
) {
// Special cases are properties that should be a basic object
if (
propName !== 'default' &&
propName !== 'examples' &&
propName !== 'const' &&
propName !== 'enum'
propName === 'properties' ||
propName === 'patternProperties' ||
propName === 'definitions' ||
propName === 'dependencies'
) {
// Special cases are properties that should be a basic object
if (
propName === 'properties' ||
propName === 'patternProperties' ||
propName === 'definitions' ||
propName === 'dependencies'
) {
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = Draft7Schema.internalToSchema(prop2, seenSchemas);
}
} else {
copyProp = Draft7Schema.internalToSchema(prop, seenSchemas);
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = Draft7Schema.internalToSchema(
prop2,
seenSchemas
);
}
}
(schema as any)[String(propName)] = copyProp;
} else {
copyProp = Draft7Schema.internalToSchema(prop, seenSchemas);
}
}
(schema as any)[String(propName)] = copyProp;
}
return schema;
}
Expand Down
11 changes: 9 additions & 2 deletions src/models/OpenapiV3Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export class OpenapiV3Schema {
'Could not convert input to expected copy of OpenapiV3Schema'
);
}

// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, OpenapiV3Schema> = new Map()
Expand Down Expand Up @@ -151,7 +153,9 @@ export class OpenapiV3Schema {
const schema = new OpenapiV3Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
if (prop === undefined) {
continue;
}
let copyProp: any = prop;

// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
Expand All @@ -176,7 +180,10 @@ export class OpenapiV3Schema {
// Special cases are properties that should be a basic object
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = OpenapiV3Schema.internalToSchema(prop2, seenSchemas);
copyProp[String(propName2)] = OpenapiV3Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = OpenapiV3Schema.internalToSchema(prop, seenSchemas);
Expand Down
11 changes: 9 additions & 2 deletions src/models/SwaggerV2Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class SwaggerV2Schema {
'Could not convert input to expected copy of SwaggerV2Schema'
);
}

// eslint-disable-next-line sonarjs/cognitive-complexity
private static internalToSchema(
object: any,
seenSchemas: Map<any, SwaggerV2Schema> = new Map()
Expand Down Expand Up @@ -118,7 +120,9 @@ export class SwaggerV2Schema {
const schema = new SwaggerV2Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
if(prop === undefined) continue;
if (prop === undefined) {
continue;
}
let copyProp: any = prop;

// Ignore value properties (those with `any` type) as they should be saved as is regardless of value
Expand All @@ -140,7 +144,10 @@ export class SwaggerV2Schema {
// Special cases are properties that should be a basic object
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = SwaggerV2Schema.internalToSchema(prop2, seenSchemas);
copyProp[String(propName2)] = SwaggerV2Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = SwaggerV2Schema.internalToSchema(prop, seenSchemas);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/Partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
export type DeepPartial<T> = T extends Function
? T
: T extends object
? { [P in keyof T]?: DeepPartial<T[P]> }
: T;
? { [P in keyof T]?: DeepPartial<T[P]> }
: T;

/**
* Return true or false based on whether the input object is a regular object or a class
Expand Down
10 changes: 6 additions & 4 deletions test/TestUtils/GeneralUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-undef */
/* eslint-disable no-undef, security/detect-object-injection */
import { promisify } from 'util';
import { exec } from 'child_process';
const promiseExec = promisify(exec);
Expand Down Expand Up @@ -30,8 +30,10 @@ export async function execCommand(
* Ensure object does not contain undefined properties when matching outputs
*/
export function removeEmptyPropertiesFromObjects(obj: any): any {
if(typeof obj !== 'object') return obj;
Object.keys(obj).forEach(key => {
if (typeof obj !== 'object') {
return obj;
}
for (const key of Object.keys(obj)) {
if (obj[key] && typeof obj[key] === 'object') {
removeEmptyPropertiesFromObjects(obj[key]);
if (Object.keys(obj[key]).length === 0) {
Expand All @@ -40,6 +42,6 @@ export function removeEmptyPropertiesFromObjects(obj: any): any {
} else if (obj[key] === undefined) {
delete obj[key];
}
});
}
return obj;
}
5 changes: 3 additions & 2 deletions test/interpreter/Intepreter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CommonModel } from '../../src';
import { Interpreter } from '../../src/interpreter/Interpreter';
import { removeEmptyPropertiesFromObjects } from '../TestUtils/GeneralUtils';

Expand All @@ -15,6 +14,8 @@ describe('Interpreter', () => {
};
const interpreter = new Interpreter();
const interpretedModel = interpreter.interpret(schema);
expect(removeEmptyPropertiesFromObjects(interpretedModel)).toMatchSnapshot();
expect(
removeEmptyPropertiesFromObjects(interpretedModel)
).toMatchSnapshot();
});
});
Loading

0 comments on commit fd80d54

Please sign in to comment.