Skip to content

Commit

Permalink
update test and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jul 25, 2024
1 parent 9f9fc78 commit 75a245e
Show file tree
Hide file tree
Showing 17 changed files with 18,331 additions and 5,377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ public partial class Root
get { return email; }
set { this.email = value; }
}
public string Serialize()
{
return this.Serialize(null);
}
public string Serialize(JsonSerializerOptions options = null)
return JsonSerializer.Serialize(this, options);
}
public static Root Deserialize(string json)
{
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new RootConverter());
return JsonSerializer.Deserialize<Root>(json, deserializeOptions);
}
}
internal class RootConverter : JsonConverter<Root>
Expand Down
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
19 changes: 16 additions & 3 deletions src/models/AsyncapiV2Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class AsyncapiV2Schema {
deprecated?: boolean;
//Extensions
[k: string]: any; // eslint-disable-line no-undef

/**
* Takes a deep copy of the input object and converts it to an instance of AsyncapiV2Schema.
*
Expand Down Expand Up @@ -124,7 +124,8 @@ 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 +139,20 @@ 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
16 changes: 13 additions & 3 deletions src/models/CommonModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ 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
18 changes: 16 additions & 2 deletions src/models/Draft4Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,25 @@ 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
18 changes: 16 additions & 2 deletions src/models/Draft6Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ 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 +94,20 @@ 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
37 changes: 25 additions & 12 deletions src/models/Draft7Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,31 @@ export class Draft7Schema {
const schema = new Draft7Schema();
seenSchemas.set(object, schema);
for (const [propName, prop] of Object.entries(object)) {
let copyProp = 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);
}
(schema as any)[String(propName)] = copyProp;
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 === '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;
}
return schema;
}
Expand Down
17 changes: 15 additions & 2 deletions src/models/OpenapiV3Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ 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 +167,20 @@ 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
17 changes: 15 additions & 2 deletions src/models/SwaggerV2Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ 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 +131,20 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ exports[`JsonBinPack preset should work fine with AsyncAPI inputs 1`] = `
}
public async jsonbinSerialize(): Promise<Buffer>{
const jsonData = JSON.parse(this.marshal());
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"type\\":\\"object\\",\\"properties\\":{\\"email\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"x-parser-schema-id\\":\\"<anonymous-schema-2>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_2\\"}},\\"x-parser-schema-id\\":\\"<anonymous-schema-1>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_1\\",\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\"});
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"type\\":\\"object\\",\\"properties\\":{\\"email\\":{\\"format\\":\\"email\\",\\"type\\":\\"string\\",\\"x-parser-schema-id\\":\\"<anonymous-schema-2>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_2\\"}},\\"x-parser-schema-id\\":\\"<anonymous-schema-1>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_1\\"});
return jsonbinpack.serialize(jsonbinpackEncodedSchema, jsonData);
}
public static async jsonbinDeserialize(buffer: Buffer): Promise<AnonymousSchema_1> {
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"type\\":\\"object\\",\\"properties\\":{\\"email\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"x-parser-schema-id\\":\\"<anonymous-schema-2>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_2\\"}},\\"x-parser-schema-id\\":\\"<anonymous-schema-1>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_1\\",\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\"});
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"type\\":\\"object\\",\\"properties\\":{\\"email\\":{\\"format\\":\\"email\\",\\"type\\":\\"string\\",\\"x-parser-schema-id\\":\\"<anonymous-schema-2>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_2\\"}},\\"x-parser-schema-id\\":\\"<anonymous-schema-1>\\",\\"x-modelgen-inferred-name\\":\\"anonymous_schema_1\\"});
const json = jsonbinpack.deserialize(jsonbinpackEncodedSchema, buffer);
return AnonymousSchema_1.unmarshal(json);
}
Expand Down Expand Up @@ -116,12 +116,12 @@ exports[`JsonBinPack preset should work fine with JSON Schema draft 4 1`] = `
}
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\\",\\"type\\":\\"object\\",\\"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\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
return jsonbinpack.serialize(jsonbinpackEncodedSchema, jsonData);
}
public static async jsonbinDeserialize(buffer: Buffer): Promise<Root> {
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"type\\":\\"object\\",\\"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\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
const json = jsonbinpack.deserialize(jsonbinpackEncodedSchema, buffer);
return Root.unmarshal(json);
}
Expand Down Expand Up @@ -180,12 +180,12 @@ exports[`JsonBinPack preset should work fine with JSON Schema draft 6 1`] = `
}
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\\",\\"type\\":\\"object\\",\\"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\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
return jsonbinpack.serialize(jsonbinpackEncodedSchema, jsonData);
}
public static async jsonbinDeserialize(buffer: Buffer): Promise<Root> {
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"https://json-schema.org/draft/2020-12/schema\\",\\"type\\":\\"object\\",\\"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\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
const json = jsonbinpack.deserialize(jsonbinpackEncodedSchema, buffer);
return Root.unmarshal(json);
}
Expand Down
10 changes: 1 addition & 9 deletions test/interpreter/Intepreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ describe('Interpreter', () => {
};
const interpreter = new Interpreter();
const interpretedModel = interpreter.interpret(schema);
expect(interpretedModel).not.toBeUndefined();
const expectedModel = new CommonModel();
expectedModel.$id = 'schema1';
const expectedPropertyModel = new CommonModel();
expectedPropertyModel.$id = 'schema2';
expectedModel.properties = {
testProp: expectedPropertyModel
};
expect(interpretedModel).toMatchObject(expectedModel);
expect(interpretedModel).toMatchSnapshot();
});
});
Loading

0 comments on commit 75a245e

Please sign in to comment.