Skip to content

Commit ec4430e

Browse files
authored
[rust] Log a sanitized identifier only when it differs from the cased original (#25015)
* [rust] Log a sanitized identifier only when it differs from the cased original * [rust] Keep the rename warning test to its own log events
1 parent 2f13add commit ec4430e

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRustCodegen.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,19 @@ public String sanitizeIdentifier(String name, CasingType casingType, String esca
201201
// Sanitize any other special characters that weren't replaced
202202
name = sanitizeName(name);
203203

204-
// Keep track of modifications prior to casing
205-
boolean nameWasModified = !originalName.equals(name);
206-
207204
// Convert casing
208205
name = casingFunction.apply(name);
209206

210207
// If word starts with number add a prefix
211208
// Note: this must be done after casing since CamelCase will strip leading underscores
212209
if (name.matches("^\\d.*")) {
213-
nameWasModified = true;
214210
name = casingFunction.apply(escapePrefix + '_' + name);
215211
}
216212

217213
// Escape reserved words - this is case-sensitive so must be done after casing
218214
// There is currently a bug in Rust where this doesn't work for a few reserved words :(
219215
// https://internals.rust-lang.org/t/raw-identifiers-dont-work-for-all-identifiers/9094
220216
if (isReservedWord(name)) {
221-
nameWasModified = true;
222217
if (this.keywordsThatDoNotSupportRawIdentifiers.contains(name) || !allowRawIdentifiers) {
223218
name = casingFunction.apply(escapePrefix + '_' + name);
224219
} else {
@@ -227,8 +222,9 @@ public String sanitizeIdentifier(String name, CasingType casingType, String esca
227222
}
228223

229224
// If the name had to be modified (not just because of casing), log the change
230-
if (nameWasModified) {
231-
LOGGER.warn("{} cannot be used as a {} name. Renamed to {}", casingFunction.apply(originalName), type, name);
225+
String casedOriginalName = casingFunction.apply(originalName);
226+
if (!casedOriginalName.equals(name)) {
227+
LOGGER.warn("{} cannot be used as a {} name. Renamed to {}", casedOriginalName, type, name);
232228
}
233229

234230
return name;

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/rust/AbstractRustCodegenTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package org.openapitools.codegen.rust;
22

3+
import ch.qos.logback.classic.Logger;
4+
import ch.qos.logback.classic.spi.ILoggingEvent;
5+
import ch.qos.logback.core.filter.Filter;
6+
import ch.qos.logback.core.read.ListAppender;
7+
import ch.qos.logback.core.spi.FilterReply;
38
import org.openapitools.codegen.CodegenProperty;
49
import org.openapitools.codegen.languages.AbstractRustCodegen;
10+
import org.slf4j.LoggerFactory;
511
import org.testng.Assert;
612
import org.testng.annotations.Test;
713

@@ -74,6 +80,32 @@ public void testSanitizeIdentifier() {
7480
Assert.assertEquals(sanitizeSnakeCase.apply(""), "");
7581
}
7682

83+
@Test
84+
public void testSanitizeIdentifierWarnsOnlyOnRename() {
85+
Logger logger = (Logger) LoggerFactory.getLogger(AbstractRustCodegen.class);
86+
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
87+
// Other test classes run in parallel and log on the same logger, so keep only this test's names
88+
listAppender.addFilter(new Filter<ILoggingEvent>() {
89+
@Override
90+
public FilterReply decide(ILoggingEvent event) {
91+
return event.getFormattedMessage().contains("SanitizeWarningProbe") ? FilterReply.NEUTRAL : FilterReply.DENY;
92+
}
93+
});
94+
listAppender.start();
95+
logger.addAppender(listAppender);
96+
try {
97+
codegen.sanitizeIdentifier("sanitize-warning-probe", AbstractRustCodegen.CasingType.CAMEL_CASE, "p", "model", true);
98+
Assert.assertTrue(listAppender.list.isEmpty());
99+
100+
codegen.sanitizeIdentifier("sanitize/warning/probe", AbstractRustCodegen.CasingType.CAMEL_CASE, "p", "model", true);
101+
Assert.assertEquals(listAppender.list.size(), 1);
102+
Assert.assertEquals(listAppender.list.get(0).getFormattedMessage(),
103+
"SanitizeWarningProbe cannot be used as a model name. Renamed to SanitizeSlashWarningSlashProbe");
104+
} finally {
105+
logger.detachAppender(listAppender);
106+
}
107+
}
108+
77109
@Test
78110
public void testToVarName() {
79111
// Should be converted to snake case

0 commit comments

Comments
 (0)