Skip to content

Commit

Permalink
fix: correct conversion of AsyncAPI types into Java types (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalelane authored Oct 24, 2022
1 parent 0b41c7c commit 27131b0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 42 deletions.
15 changes: 9 additions & 6 deletions test/Types.utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ const crypto = require('crypto');

const MAIN_TEST_RESULT_PATH = path.join('test', 'temp', 'integrationTestResult');

// Test class asyncApiTypeToJavaType
// Test class asyncApiToJavaType
test('Check integer type is converted to int', () => {
expect(typesUtils.asyncApiTypeToJavaType('integer')).toBe('int');
expect(typesUtils.asyncApiToJavaType('integer')).toBe('int');
});
test('Check int64 type is converted to long', () => {
expect(typesUtils.asyncApiToJavaType('integer', 'int64')).toBe('long');
});

test('Check string type is converted to String', () => {
expect(typesUtils.asyncApiTypeToJavaType('string')).toBe('String');
expect(typesUtils.asyncApiToJavaType('string')).toBe('String');
});

test('Check password type is converted to String', () => {
expect(typesUtils.asyncApiTypeToJavaType('password')).toBe('String');
expect(typesUtils.asyncApiToJavaType('string', 'password')).toBe('String');
});

test('Check byte type is not changed', () => {
expect(typesUtils.asyncApiTypeToJavaType('byte')).toBe('byte');
expect(typesUtils.asyncApiToJavaType('string', 'byte')).toBe('byte');
});

test('Unexpected type throws error', () => {
expect(() => { typesUtils.asyncApiTypeToJavaType('test');}).toThrow();
expect(() => { typesUtils.asyncApiToJavaType('test');}).toThrow();
});

// Test class setLocalVariables
Expand Down
87 changes: 51 additions & 36 deletions utils/Types.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,61 @@
* limitations under the License.
*/

/*
* Converts from async api defined types (https://www.asyncapi.com/docs/specifications/v2.0.0#dataTypeFormat)
/*
* Converts from async api defined types (https://www.asyncapi.com/docs/specifications/v2.5.0#dataTypeFormat)
* to native Java types
*/
export function asyncApiTypeToJavaType(asyncApiType) {
switch (asyncApiType) {
case 'integer':
return 'int';

case 'long':
return 'Long';

case 'float':
return 'float';

case 'double':
return 'double';

case 'string':
return 'String';

case 'byte':
return 'byte';

case 'binary':
return 'String';
export function asyncApiToJavaType(type, format) {
// try to use the format first, as it is more specific
let asyncApiType = asyncApiFormatToJavaType(format);

case 'boolean':
return 'boolean';
if (!asyncApiType) {
// no recognised format, so resort to using the type
asyncApiType = asyncApiTypeToJavaType(type);
}

case 'date':
return 'String';
if (!asyncApiType) {
// still nothing recognised, so need to report an error
throw new Error('Unsupported Type');
}

case 'dateTime':
return 'String';
return asyncApiType;
}

case 'password':
return 'String';
default:
throw new Error('Unsupported Type');
function asyncApiFormatToJavaType(format) {
switch (format) {
case 'int32':
return 'int';
case 'int64':
return 'long';
case 'float':
return 'float';
case 'double':
return 'double';
case 'byte':
return 'byte';
case 'binary':
return 'String';
case 'date':
return 'String';
case 'date-time':
return 'String';
case 'password':
return 'String';
}
}
function asyncApiTypeToJavaType(type) {
switch (type) {
case 'integer':
return 'int';
case 'number':
// using double by default, as no format
// was specified
return 'double';
case 'string':
return 'String';
case 'boolean':
return 'boolean';
}
}

Expand All @@ -73,7 +88,7 @@ export function setLocalVariables(properties) {
*/
export function defineVariablesForProperties(properties) {
return Object.entries(properties).map(([name, property]) => {
return `public ${asyncApiTypeToJavaType(property.type())} ${name};`;
return `public ${asyncApiToJavaType(property.type(), property.format())} ${name};`;
});
}

Expand All @@ -91,7 +106,7 @@ export function passJavaArgs(properties) {
*/
export function createJavaArgsFromProperties(properties) {
return Object.entries(properties).map(([name, property]) => {
return `${asyncApiTypeToJavaType(property.type())} ${name}`;
return `${asyncApiToJavaType(property.type(), property.format())} ${name}`;
});
}

Expand Down

0 comments on commit 27131b0

Please sign in to comment.