Skip to content

Commit

Permalink
fix: empty object generation (#347)
Browse files Browse the repository at this point in the history
* fix: empty object generation

* remove code duplication
  • Loading branch information
Tenischev committed Dec 13, 2023
1 parent 287a6bc commit 2216c9c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
10 changes: 9 additions & 1 deletion template/src/main/java/com/asyncapi/model/$$objectSchema$$.java
Expand Up @@ -156,8 +156,11 @@ public class {{allName}} {
this.{{varName}} = {{varName}};
}
{% endfor %}
{% if params.disableEqualsHashCode === 'false' %}@Override
{% if params.disableEqualsHashCode === 'false' %}@Override{% set hasProps = schema.properties() | length > 0 %}
public boolean equals(Object o) {
{%- if not hasProps %}
return super.equals(o);
{% else %}
if (this == o) {
return true;
}
Expand All @@ -167,11 +170,16 @@ public boolean equals(Object o) {
{{schemaName | camelCase | upperFirst}} {{schemaName | camelCase}} = ({{schemaName | camelCase | upperFirst}}) o;
return {% for propName, prop in schema.properties() %}{% set varName = propName | camelCase %}
Objects.equals(this.{{varName}}, {{schemaName | camelCase}}.{{varName}}){% if not loop.last %} &&{% else %};{% endif %}{% endfor %}
{% endif -%}
}

@Override
public int hashCode() {
{%- if not hasProps %}
return super.hashCode();
{% else %}
return Objects.hash({% for propName, prop in schema.properties() %}{{propName | camelCase}}{% if not loop.last %}, {% endif %}{% endfor %});
{% endif -%}
}{% endif %}

@Override
Expand Down
53 changes: 53 additions & 0 deletions tests/__snapshots__/map-format.test.js.snap
Expand Up @@ -452,3 +452,56 @@ public class Interpret {
}
}"
`;

exports[`template integration tests for map format should generate correct DTO for empty object 1`] = `
"package com.asyncapi.model;
import jakarta.validation.constraints.*;
import jakarta.validation.Valid;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import javax.annotation.processing.Generated;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Test correct generation of object without parameters
*/
@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate")
public class EmptyObject {
@Override
public boolean equals(Object o) {
return super.equals(o);
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public String toString() {
return "class EmptyObject {\\n" +
"}";
}
/**
* Convert the given object to string with each line indented by 4 spaces (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\\n", "\\n ");
}
}"
`;
18 changes: 18 additions & 0 deletions tests/map-format.test.js
Expand Up @@ -33,4 +33,22 @@ describe('template integration tests for map format', () => {
expect(fileWithAnyDate).toMatchSnapshot();
}
});

it('should generate correct DTO for empty object', async() => {
const outputDir = generateFolderName();
const params = {};
const mapFormatExamplePath = './mocks/map-format.yml';

const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('tests', mapFormatExamplePath));

const expectedFiles = [
'/src/main/java/com/asyncapi/model/EmptyObject.java',
];
for (const index in expectedFiles) {
const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');
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"');
expect(fileWithAnyDate).toMatchSnapshot();
}
});
});
3 changes: 3 additions & 0 deletions tests/mocks/map-format.yml
Expand Up @@ -76,6 +76,9 @@ components:
additionalProperties:
$ref: '#/components/schemas/Interpret'
schemas:
emptyObject:
type: object
description: Test correct generation of object without parameters
Album:
description: Album
type: object
Expand Down

0 comments on commit 2216c9c

Please sign in to comment.