Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty object generation #347

Merged
merged 8 commits into from
Dec 13, 2023
Merged
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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