Skip to content

Commit

Permalink
fix: jsonbinpack preset and runtime tests (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jan 10, 2024
1 parent 651eaeb commit 7cdee96
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Array [
return instance;
}
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\\"});
Expand Down
9 changes: 4 additions & 5 deletions src/generators/typescript/presets/JsonBinPackPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ function getInputSchema(originalInput: any): string {
export const TS_JSONBINPACK_PRESET: TypeScriptPreset = {
class: {
async additionalContent({ renderer, content, model }) {
renderer.dependencyManager.addTypeScriptDependency(
'jsonbinpack',
'jsonbinpack'
renderer.dependencyManager.addDependency(
"const jsonbinpack = require('jsonbinpack')"
);

const jsonSchema = await alterschema(
model.originalInput,
getInputSchema(model.originalInput),
'2020-12'
);
jsonSchema['$schema'] = 'https://json-schema.org/draft/2020-12/schema';
const json = JSON.stringify(jsonSchema);
const packContent = `
public async jsonbinSerialize(): Promise<Buffer>{
const packContent = `public async jsonbinSerialize(): Promise<Buffer>{
const jsonData = JSON.parse(this.marshal());
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema(${json});
return jsonbinpack.serialize(jsonbinpackEncodedSchema, jsonData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ exports[`JsonBinPack preset should work fine with AsyncAPI inputs 1`] = `
}
return instance;
}
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\\"});
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\\"});
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\\"});
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 json = jsonbinpack.deserialize(jsonbinpackEncodedSchema, buffer);
return AnonymousSchema_1.unmarshal(json);
}
Expand Down Expand Up @@ -115,15 +114,14 @@ exports[`JsonBinPack preset should work fine with JSON Schema draft 4 1`] = `
}
return instance;
}
public async jsonbinSerialize(): Promise<Buffer>{
const jsonData = JSON.parse(this.marshal());
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"http://json-schema.org/draft-04/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\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"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\\":\\"http://json-schema.org/draft-04/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\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"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,15 +178,14 @@ exports[`JsonBinPack preset should work fine with JSON Schema draft 6 1`] = `
}
return instance;
}
public async jsonbinSerialize(): Promise<Buffer>{
const jsonData = JSON.parse(this.marshal());
const jsonbinpackEncodedSchema = await jsonbinpack.compileSchema({\\"$schema\\":\\"http://json-schema.org/draft-06/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\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"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\\":\\"http://json-schema.org/draft-06/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\\":{\\"type\\":\\"string\\",\\"format\\":\\"email\\",\\"x-modelgen-inferred-name\\":\\"email\\"}},\\"x-modelgen-inferred-name\\":\\"root\\"});
const json = jsonbinpack.deserialize(jsonbinpackEncodedSchema, buffer);
return Root.unmarshal(json);
}
Expand Down
25 changes: 23 additions & 2 deletions test/runtime/runtime-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, TypeScriptFileGenerator } from
import path from 'path';
import input from './generic-input.json';


async function generateNamedExport() {
const generator = new TypeScriptFileGenerator({});

Expand All @@ -16,6 +15,7 @@ async function generateNamedExport() {
{ exportType: 'named' }
);
}

async function generateDefaultExport() {
const generator = new TypeScriptFileGenerator({});

Expand Down Expand Up @@ -63,8 +63,29 @@ async function generateJsonBinPack() {
TS_JSONBINPACK_PRESET
]
});
const simplifiedInput = {
"$id": "Address",
"description": "This object contains all types of MetaModel generations",
"type": "object",
"properties": {
"street_name": {
"type": "string"
},
"house_number": {
"type": "number"
},
"marriage": {
"type": "boolean",
"description": "Status if marriage live in given house"
},
},
"required": [
"street_name",
"house_number",
]
}
await generator.generateToFiles(
input,
simplifiedInput,
path.resolve(
// eslint-disable-next-line no-undef
__dirname,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Address } from '../src/jsonbinpack/Address';
import { NestedObject } from '../src/jsonbinpack/NestedObject';

describe('Address', () => {
const nestedObj = new NestedObject({
test: 'test'
});
const address = new Address({
streetName: 'test',
houseNumber: 1,
marriage: true,
members: 2,
arrayType: [1, 'test'],
nestedObject: nestedObj
marriage: true
});
test('be able to serialize model and turning it back to a model with the same values', async () => {
const serialized = await address.jsonbinSerialize();
Expand Down

0 comments on commit 7cdee96

Please sign in to comment.