Skip to content

Commit 2216c9c

Browse files
authored
fix: empty object generation (#347)
* fix: empty object generation * remove code duplication
1 parent 287a6bc commit 2216c9c

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

template/src/main/java/com/asyncapi/model/$$objectSchema$$.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ public class {{allName}} {
156156
this.{{varName}} = {{varName}};
157157
}
158158
{% endfor %}
159-
{% if params.disableEqualsHashCode === 'false' %}@Override
159+
{% if params.disableEqualsHashCode === 'false' %}@Override{% set hasProps = schema.properties() | length > 0 %}
160160
public boolean equals(Object o) {
161+
{%- if not hasProps %}
162+
return super.equals(o);
163+
{% else %}
161164
if (this == o) {
162165
return true;
163166
}
@@ -167,11 +170,16 @@ public boolean equals(Object o) {
167170
{{schemaName | camelCase | upperFirst}} {{schemaName | camelCase}} = ({{schemaName | camelCase | upperFirst}}) o;
168171
return {% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}
169172
Objects.equals(this.{{varName}}, {{schemaName | camelCase}}.{{varName}}){% if not loop.last %} &&{% else %};{% endif %}{% endfor %}
173+
{% endif -%}
170174
}
171175

172176
@Override
173177
public int hashCode() {
178+
{%- if not hasProps %}
179+
return super.hashCode();
180+
{% else %}
174181
return Objects.hash({% for propName, prop in schema.properties() %}{{propName | camelCase}}{% if not loop.last %}, {% endif %}{% endfor %});
182+
{% endif -%}
175183
}{% endif %}
176184

177185
@Override

tests/__snapshots__/map-format.test.js.snap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,56 @@ public class Interpret {
452452
}
453453
}"
454454
`;
455+
456+
exports[`template integration tests for map format should generate correct DTO for empty object 1`] = `
457+
"package com.asyncapi.model;
458+
459+
460+
import jakarta.validation.constraints.*;
461+
import jakarta.validation.Valid;
462+
463+
import com.fasterxml.jackson.annotation.JsonCreator;
464+
import com.fasterxml.jackson.annotation.JsonProperty;
465+
import com.fasterxml.jackson.annotation.JsonValue;
466+
467+
import javax.annotation.processing.Generated;
468+
import java.util.List;
469+
import java.util.Map;
470+
import java.util.Objects;
471+
472+
/**
473+
* Test correct generation of object without parameters
474+
*/
475+
@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate")
476+
public class EmptyObject {
477+
478+
479+
480+
@Override
481+
public boolean equals(Object o) {
482+
return super.equals(o);
483+
}
484+
485+
@Override
486+
public int hashCode() {
487+
return super.hashCode();
488+
}
489+
490+
@Override
491+
public String toString() {
492+
return "class EmptyObject {\\n" +
493+
494+
"}";
495+
}
496+
497+
/**
498+
* Convert the given object to string with each line indented by 4 spaces (except the first line).
499+
*/
500+
private String toIndentedString(Object o) {
501+
if (o == null) {
502+
return "null";
503+
}
504+
return o.toString().replace("\\n", "\\n ");
505+
}
506+
}"
507+
`;

tests/map-format.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,22 @@ describe('template integration tests for map format', () => {
3333
expect(fileWithAnyDate).toMatchSnapshot();
3434
}
3535
});
36+
37+
it('should generate correct DTO for empty object', async() => {
38+
const outputDir = generateFolderName();
39+
const params = {};
40+
const mapFormatExamplePath = './mocks/map-format.yml';
41+
42+
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
43+
await generator.generateFromFile(path.resolve('tests', mapFormatExamplePath));
44+
45+
const expectedFiles = [
46+
'/src/main/java/com/asyncapi/model/EmptyObject.java',
47+
];
48+
for (const index in expectedFiles) {
49+
const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');
50+
const fileWithAnyDate = file.replace(/date="\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)"/, 'date="AnyDate"');
51+
expect(fileWithAnyDate).toMatchSnapshot();
52+
}
53+
});
3654
});

tests/mocks/map-format.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ components:
7676
additionalProperties:
7777
$ref: '#/components/schemas/Interpret'
7878
schemas:
79+
emptyObject:
80+
type: object
81+
description: Test correct generation of object without parameters
7982
Album:
8083
description: Album
8184
type: object

0 commit comments

Comments
 (0)