Skip to content

Commit aa867fb

Browse files
committed
fix: use parameter generateTimestamp to suppress timestamp in @generated annotation #433
Signed-off-by: anessi <16045045+anessi@users.noreply.github.com>
1 parent 4e2766b commit aa867fb

6 files changed

Lines changed: 46 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ It is RECOMMENDED to not use anonymous objects in payload and components definit
8787
|completionTimeout|Only for MQTT. The completion timeout in milliseconds for operations. The default completion timeout is 30000 milliseconds.| No | `30000` |
8888
|mqttClientId| Only for MQTT. Provides the client identifier for the MQTT server. This parameter overrides the value of the clientId if it's set in the AsyncAPI file.If both aren't provided, a default value is set.| No | |
8989
|asyncapiFileDir| Path where original AsyncAPI file will be stored.| No | `src/main/resources/api/` |
90+
|generateTimestamp| Adds the generation timestamp to the @Generated annotation.| No | `true` |
9091
### Examples
9192

9293
The shortest possible syntax:

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,14 @@
117117
"description": "Generate pom.xml Maven build file instead of Gradle build",
118118
"default": false,
119119
"required": false
120+
},
121+
"generateTimestamp": {
122+
"description": "Adds the generation timestamp to the @Generated annotation",
123+
"default": "true",
124+
"required": false
120125
}
121126
},
122-
"generator": ">=1.8.27 <2.0.0",
127+
"generator": ">=1.8.27",
123128
"filters": [
124129
"@asyncapi/generator-filters"
125130
],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* {{ line | safe}}{% endfor %}{% if message.examples() %}
1515
* Examples: {{message.examples() | examplesToString | safe}}{% endif %}
1616
*/{% endif %}
17-
@Generated(value="com.asyncapi.generator.template.spring", date="{{''|currentTime }}")
17+
@Generated(value="com.asyncapi.generator.template.spring"{% if params.generateTimestamp === 'true' %}, date="{{''|currentTime }}"{%- endif %})
1818
public class {{messageName | camelCase | upperFirst}} {
1919
{%- if message.payload().anyOf() or message.payload().oneOf() %}
2020
{%- set payloadName = 'OneOf' %}{%- set hasPrimitive = false %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* {{ line | safe}}{% endfor %}{% if schema.examples() %}
2222
* Examples: {{schema.examples() | examplesToString | safe}}{% endif %}
2323
*/{% endif %}
24-
@Generated(value="com.asyncapi.generator.template.spring", date="{{''|currentTime }}")
24+
@Generated(value="com.asyncapi.generator.template.spring"{% if params.generateTimestamp === 'true' %}, date="{{''|currentTime }}"{%- endif %})
2525
public class {{schemaName | camelCase | upperFirst}} {
2626
{% for propName, prop in schema.properties() %}
2727
{%- set isRequired = propName | isRequired(schema.required()) %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{% if parameter.hasDescription() %}/**{% for line in parameter.description() | splitByLines %}
2121
* {{ line | safe}}{% endfor %}
2222
*/{% endif %}
23-
@Generated(value="com.asyncapi.generator.template.spring", date="{{''|currentTime }}")
23+
@Generated(value="com.asyncapi.generator.template.spring"{% if params.generateTimestamp === 'true' %}, date="{{''|currentTime }}"{%- endif %})
2424
public class {{parameterName | camelCase | upperFirst}} {
2525
{% set schema = parameter.schema() %}
2626
{% for propName, prop in schema.properties() %}

tests/parameters.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,40 @@ describe('integration tests for generated files under different template paramet
8686
expect(existsSync(path.join(outputDir, notExpectedFiles[index]))).toBeFalsy();
8787
}
8888
});
89+
90+
it('should include timestamp in @Generated annotation when generateTimestamp is true', async () => {
91+
const outputDir = generateFolderName();
92+
const params = { generateTimestamp: 'true' };
93+
const kafkaExamplePath = './mocks/kafka.yml';
94+
95+
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
96+
await generator.generateFromFile(path.resolve('tests', kafkaExamplePath));
97+
98+
const filesToCheck = [
99+
'/src/main/java/com/asyncapi/model/LightMeasured.java',
100+
'/src/main/java/com/asyncapi/model/LightMeasuredPayload.java'
101+
];
102+
for (const index in filesToCheck) {
103+
const generatedFile = await readFile(path.join(outputDir, filesToCheck[index]), 'utf8');
104+
expect(generatedFile).toMatch(/date=".*"/);
105+
}
106+
});
107+
108+
it('should not include timestamp in @Generated annotation when generateTimestamp is false', async () => {
109+
const outputDir = generateFolderName();
110+
const params = { generateTimestamp: 'false' };
111+
const kafkaExamplePath = './mocks/kafka.yml';
112+
113+
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
114+
await generator.generateFromFile(path.resolve('tests', kafkaExamplePath));
115+
116+
const filesToCheck = [
117+
'/src/main/java/com/asyncapi/model/LightMeasured.java',
118+
'/src/main/java/com/asyncapi/model/LightMeasuredPayload.java'
119+
];
120+
for (const index in filesToCheck) {
121+
const generatedFile = await readFile(path.join(outputDir, filesToCheck[index]), 'utf8');
122+
expect(generatedFile).not.toMatch(/date=".*"/);
123+
}
124+
});
89125
});

0 commit comments

Comments
 (0)