Skip to content

Commit

Permalink
refactor: fixed spacing for java classes; skipping one test that no l…
Browse files Browse the repository at this point in the history
…onger works for classes to be generated with the same name; cleaned up some code
  • Loading branch information
CameronRushton committed Jan 8, 2024
1 parent 55a25c6 commit 5bcfc19
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 57 deletions.
4 changes: 0 additions & 4 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,10 +969,6 @@ function getMessagePayloadType(message) {

if (!type || type === 'object') {
ret = applicationModel.getModelClass({ schemaName: payload.ext('x-parser-schema-id') }).getClassName();
// ret = payload.ext('x-parser-schema-id');
// const { className } = scsLib.stripPackageName(ret);
// ret = _.camelCase(className);
// ret = _.upperFirst(ret);
} else {
ret = getType(type, payload.format()).javaType;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/applicationModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ApplicationModel {
if (this.topLevelSchemasByParserSchemaId) {
return;
}
// There may be schemas as part of channels directly. It's okay if we don't find them, but it would make things more obvious for the template.
const components = ApplicationModel.asyncapi._json.components;
const topLevelSchemaNames = Object.keys(components?.schemas || {});
const topLevelSchemas = topLevelSchemaNames.map(schemaName => {
Expand Down Expand Up @@ -190,14 +191,10 @@ class ApplicationModel {

// Is this schema at the top level? Try matching the x-parser-schema-id
const topLevelSchema = this.topLevelSchemasByParserSchemaId[schema._json["x-parser-schema-id"]];
// if (topLevelSchemas.includes(schemaName)) {
// modelClass.setCanBeInnerClass(false);
// }
let className = "";
let javaPackage = "";
if (topLevelSchema) {
// We set and flattened this earlier. { rootSchemaName: { id: 123 }} -> [{ x_template_schema_name: "rootSchemaName", id: 123 }]
modelClass.setCanBeInnerClass(false); // TODO: Remove
({ className, javaPackage } = this.getClassNameAndPackage(schema, topLevelSchema.x_template_schema_name));
} else {
({ className, javaPackage } = this.getClassNameAndPackage(schema, schemaName));
Expand Down Expand Up @@ -237,6 +234,8 @@ class ApplicationModel {
val.anonymousSchemaToSubClassMap = null;
val.modelClassMap = null;
val.nameToSchemaMap = null;
val.isFinishedSettingClassNames = null;
val.topLevelSchemasByParserSchemaId = null;
});
}
}
Expand Down
9 changes: 0 additions & 9 deletions lib/modelClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const _ = require('lodash');

class ModelClass {
constructor() {
this.innerClass = true;
}

getClassName() {
Expand Down Expand Up @@ -37,14 +36,6 @@ class ModelClass {
let fixedName = originalName.substring(originalName.lastIndexOf("/") + 1);
return _.upperFirst(_.camelCase(fixedName));
}

setCanBeInnerClass(innerClass) {
this.innerClass = innerClass;
}

canBeInnerClass() {
return this.innerClass;
}
}

module.exports = ModelClass;
4 changes: 2 additions & 2 deletions partials/java-class
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
{{ indent3 }}return this;
{{ indent2 }}}
{# Enums #}
{%- if prop.enum() %}
{% if prop.enum() %}
{{ indent2 }}public static enum {{ type }} { {{ prop.enum() }} }
{%- endif %}
{%- endif -%}
{%- endfor -%}

{{ indent2 }}public String toString() {
Expand Down
81 changes: 43 additions & 38 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ describe('template integration tests using the generator', () => {
outputDirectory = generateFolderName();
});

it('should place the topic variables in the correct order', async () => {
// For a topic of test/{var1}/{var2}, the listed params in the asyncapi document can be in any order
await generate('mocks/multivariable-topic.yaml');

const validatedFiles = [
'src/main/java/Application.java'
];
await assertExpectedFiles(validatedFiles);
});

it('should generate application files using the solace binder', async () => {
const params = {
binder: 'solace',
Expand Down Expand Up @@ -110,6 +120,11 @@ describe('template integration tests using the generator', () => {
await assertExpectedFiles(validatedFiles);
});

/*
The generator only makes 1 User class which gets moved to the avro package path and we'll get a FileNotFound exception
when trying to move the other one from the same place to the correct directory.
Needed two User classes.
*/
it.skip('avro schemas should appear in a package based on their namespace, if any.', async () => {
// Note that this file has 2 Avro schemas named User, but one has the namespace 'userpublisher.'
const AVRO_PACKAGE_PATH = 'userpublisher';
Expand Down Expand Up @@ -141,22 +156,6 @@ describe('template integration tests using the generator', () => {
await assertExpectedFiles(validatedFiles);
});

it('should generate schemas with nested arrays', async () => {
await generate('mocks/nested-arrays.yaml');

const validatedFiles = [
'src/main/java/Application.java',
'src/main/java/Dossier.java',
'src/main/java/Debtor.java',
'src/main/java/Address.java',
'src/main/java/BankAccount.java',
'src/main/java/Emails.java',
'src/main/java/Options.java',
'src/main/java/Phones.java'
];
await assertExpectedFiles(validatedFiles);
});

it('should generate code from the smarty lighting streetlights example', async () => {
await generate('mocks/smarty-lighting-streetlights.yaml');

Expand Down Expand Up @@ -191,22 +190,34 @@ describe('template integration tests using the generator', () => {
await assertExpectedFiles(validatedFiles);
});

it('should place the topic variables in the correct order', async () => {
// For a topic of test/{var1}/{var2}, the listed params in the asyncapi document can be in any order
it('should not populate application yml with functions that are not beans', async () => {
// If the function is a supplier or using a stream bridge, the function isn't a bean and shouldnt be in application.yml
await generate('mocks/multivariable-topic.yaml');

const validatedFiles = [
'src/main/java/Application.java'
'src/main/resources/application.yml'
];
await assertExpectedFiles(validatedFiles);
});

it('should not populate application yml with functions that are not beans', async () => {
// If the function is a supplier or using a stream bridge, the function isn't a bean and shouldnt be in application.yml
await generate('mocks/multivariable-topic.yaml');
it('should generate one class for a schema with multiple references to it in the asyncapi document', async () => {
/*
For this test, there's duplicate schemas in the asyncapi document.
Calling allSchemas() yeilds unique results (deduplicated) based on either $id or x-parser-schema-id in that order.
If the $id is duplicated in the asyncapi document, allSchemas() won't return the duplicate which may be a unique schema.
The $id needs to be changed because the generator will write file names and such based on it which will always be faulty for this code generator.
Changing the $id of one schema will change the $id only for the one instance, leaving the duplicates behind with the $id they've always had.
This means if we started with 10 schemas, 5 being duplicates with one other schema (15 total; 10 unique), we would end up with 15 unique and 15 total if we changed the schema's $id.
This test ensures all schemas and their duplicates have their $ids renamed to generate the correct file names.
*/
await generate('mocks/schemas-with-duplicate-$ids.yaml');

const validatedFiles = [
'src/main/resources/application.yml'
'src/main/java/Application.java',
'src/main/java/Driver.java',
'src/main/java/Passenger.java',
'src/main/java/PaymentCharged.java',
'src/main/java/RideUpdated1.java'
];
await assertExpectedFiles(validatedFiles);
});
Expand All @@ -225,24 +236,18 @@ describe('template integration tests using the generator', () => {
await assertExpectedFiles(validatedFiles);
});

it('should generate one class for a schema with multiple references to it in the asyncapi document', async () => {
/*
For this test, there's duplicate schemas in the asyncapi document.
Calling allSchemas() yeilds unique results (deduplicated) based on either $id or x-parser-schema-id in that order.
If the $id is duplicated in the asyncapi document, allSchemas() won't return the duplicate which may be a unique schema.
The $id needs to be changed because the generator will write file names and such based on it which will always be faulty for this code generator.
Changing the $id of one schema will change the $id only for the one instance, leaving the duplicates behind with the $id they've always had.
This means if we started with 10 schemas, 5 being duplicates with one other schema (15 total; 10 unique), we would end up with 15 unique and 15 total if we changed the schema's $id.
This test ensures all schemas and their duplicates have their $ids renamed to generate the correct file names.
*/
await generate('mocks/schemas-with-duplicate-$ids.yaml');
it('should generate schemas with nested arrays', async () => {
await generate('mocks/nested-arrays.yaml');

const validatedFiles = [
'src/main/java/Application.java',
'src/main/java/Driver.java',
'src/main/java/Passenger.java',
'src/main/java/PaymentCharged.java',
'src/main/java/RideUpdated1.java'
'src/main/java/Dossier.java',
'src/main/java/Debtor.java',
'src/main/java/Address.java',
'src/main/java/BankAccount.java',
'src/main/java/Emails.java',
'src/main/java/Options.java',
'src/main/java/Phones.java'
];
await assertExpectedFiles(validatedFiles);
});
Expand Down

0 comments on commit 5bcfc19

Please sign in to comment.