Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bundle of problems #2078

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ Array [
}
public async jsonbinSerialize(): Promise<Buffer>{
const jsonData = JSON.parse(this.marshal());
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"$id\\":\\"Test\\",\\"type\\":\\"object\\",\\"additionalProperties\\":false,\\"properties\\":{\\"email\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"x-modelgen-inferred-name\\":\\"email\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"type\\":\\"object\\",\\"properties\\":{\\"email\\":{\\"format\\":\\"email\\",\\"type\\":\\"string\\",\\"x-modelgen-inferred-name\\":\\"email\\"}},\\"additionalProperties\\":false,\\"$id\\":\\"Test\\",\\"x-modelgen-inferred-name\\":\\"root\\"});
return jsonbinpack.serialize(jsonbinpackEncodedSchema, jsonData);
}

public static async jsonbinDeserialize(buffer: Buffer): Promise<Test> {
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"$id\\":\\"Test\\",\\"type\\":\\"object\\",\\"additionalProperties\\":false,\\"properties\\":{\\"email\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"x-modelgen-inferred-name\\":\\"email\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"type\\":\\"object\\",\\"properties\\":{\\"email\\":{\\"format\\":\\"email\\",\\"type\\":\\"string\\",\\"x-modelgen-inferred-name\\":\\"email\\"}},\\"additionalProperties\\":false,\\"$id\\":\\"Test\\",\\"x-modelgen-inferred-name\\":\\"root\\"});
const json = jsonbinpack.deserialize(jsonbinpackEncodedSchema, buffer);
return Test.unmarshal(json);
}
Expand Down
9,889 changes: 4,561 additions & 5,328 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@
"docker:test": "docker compose run modelina npm run test",
"test": "npm run test:library && npm run test:examples && npm run test:cli",
"test:cli": "cd modelina-cli && npm run test",
"test:update": "npm run test:library -- -u && npm run test:examples:update",
"test:update": "npm run test:library:update && npm run test:examples:update",
"test:library": "cross-env CI=true jest --coverage --testPathIgnorePatterns ./modelina-website --testPathIgnorePatterns ./test/runtime --testPathIgnorePatterns ./examples",
"test:library:update": "npm run test:library -- -u",
"test:examples": "npm run test:examples:regular && npm run test:examples:websites",
"test:examples:update": "npm run test:examples:regular -- -u && npm run test:examples:websites -- -u",
"test:examples:regular": "cross-env CI=true jest ./examples --testPathIgnorePatterns ./examples/integrate-with-react --testPathIgnorePatterns ./examples/integrate-with-next",
Expand Down
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
24 changes: 22 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,10 @@ export class AsyncapiV2Schema {
const schema = new AsyncapiV2Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;
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 (
Expand All @@ -138,8 +143,23 @@ export class AsyncapiV2Schema {
schema.externalDocs =
AsyncapiV2ExternalDocumentation.toExternalDocumentation(prop);
continue;
} else if (
propName === 'properties' ||
propName === 'patternProperties' ||
propName === 'definitions' ||
propName === 'dependencies'
) {
// 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
);
}
} else {
copyProp = AsyncapiV2Schema.internalToSchema(prop, seenSchemas);
}
copyProp = AsyncapiV2Schema.internalToSchema(prop, seenSchemas);
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
20 changes: 17 additions & 3 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,11 +75,23 @@ export class CommonModel {
const schema = new CommonModel();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;

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') {
copyProp = CommonModel.internalToSchema(prop, seenSchemas);
if (propName === 'properties' || propName === 'patternProperties') {
copyProp = {};
for (const [propName2, prop2] of Object.entries(prop as any)) {
copyProp[String(propName2)] = CommonModel.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = CommonModel.internalToSchema(prop, seenSchemas);
}
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
24 changes: 22 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,11 +79,30 @@ export class Draft4Schema {
const schema = new Draft4Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;
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 !== 'enum') {
copyProp = Draft4Schema.internalToSchema(prop, seenSchemas);
// 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)] = Draft4Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = Draft4Schema.internalToSchema(prop, seenSchemas);
}
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
24 changes: 22 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,10 @@ export class Draft6Schema {
const schema = new Draft6Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;
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 (
Expand All @@ -93,7 +97,23 @@ export class Draft6Schema {
propName !== 'const' &&
propName !== 'enum'
) {
copyProp = Draft6Schema.internalToSchema(prop, seenSchemas);
// 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)] = Draft6Schema.internalToSchema(
prop2,
seenSchemas
);
}
} else {
copyProp = Draft6Schema.internalToSchema(prop, seenSchemas);
}
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
26 changes: 23 additions & 3 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,16 +96,34 @@ export class Draft7Schema {
const schema = new Draft7Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;

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'
) {
copyProp = Draft7Schema.internalToSchema(prop, seenSchemas);
// 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);
}
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
24 changes: 22 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,10 @@ export class OpenapiV3Schema {
const schema = new OpenapiV3Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;
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 !== 'enum') {
Expand All @@ -166,8 +171,23 @@ export class OpenapiV3Schema {
} else if (propName === 'discriminator') {
schema.discriminator = OpenapiV3Discriminator.toDiscriminator(prop);
continue;
} else if (
propName === 'properties' ||
propName === 'patternProperties' ||
propName === 'definitions' ||
propName === 'dependencies'
) {
// 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
);
}
} else {
copyProp = OpenapiV3Schema.internalToSchema(prop, seenSchemas);
}
copyProp = OpenapiV3Schema.internalToSchema(prop, seenSchemas);
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
24 changes: 22 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,10 @@ export class SwaggerV2Schema {
const schema = new SwaggerV2Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = prop;
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 !== 'enum') {
Expand All @@ -130,8 +135,23 @@ export class SwaggerV2Schema {
} else if (propName === 'xml') {
schema.xml = SwaggerV2Xml.toXml(prop);
continue;
} else if (
propName === 'properties' ||
propName === 'patternProperties' ||
propName === 'definitions' ||
propName === 'dependencies'
) {
// 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
);
}
} else {
copyProp = SwaggerV2Schema.internalToSchema(prop, seenSchemas);
}
copyProp = SwaggerV2Schema.internalToSchema(prop, seenSchemas);
}
(schema as any)[String(propName)] = copyProp;
}
Expand Down
5 changes: 1 addition & 4 deletions src/processors/AsyncAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
return alreadyIteratedSchemas.get(schemaUid) as AsyncapiV2Schema;
}

const convertedSchema = Object.assign(
new AsyncapiV2Schema(),
schema.json()
);
const convertedSchema = AsyncapiV2Schema.toSchema(schema.json());
convertedSchema[this.MODELGEN_INFFERED_NAME] = schemaUid;
alreadyIteratedSchemas.set(schemaUid, convertedSchema);

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
Loading
Loading