Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,19 @@ public String sanitizeIdentifier(String name, CasingType casingType, String esca
// Sanitize any other special characters that weren't replaced
name = sanitizeName(name);

// Keep track of modifications prior to casing
boolean nameWasModified = !originalName.equals(name);

// Convert casing
name = casingFunction.apply(name);

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

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

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

return name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.openapitools.codegen.rust;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.read.ListAppender;
import ch.qos.logback.core.spi.FilterReply;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.languages.AbstractRustCodegen;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -74,6 +80,32 @@ public void testSanitizeIdentifier() {
Assert.assertEquals(sanitizeSnakeCase.apply(""), "");
}

@Test
public void testSanitizeIdentifierWarnsOnlyOnRename() {
Logger logger = (Logger) LoggerFactory.getLogger(AbstractRustCodegen.class);
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
// Other test classes run in parallel and log on the same logger, so keep only this test's names
listAppender.addFilter(new Filter<ILoggingEvent>() {
@Override
public FilterReply decide(ILoggingEvent event) {
return event.getFormattedMessage().contains("SanitizeWarningProbe") ? FilterReply.NEUTRAL : FilterReply.DENY;
}
});
listAppender.start();
logger.addAppender(listAppender);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
try {
codegen.sanitizeIdentifier("sanitize-warning-probe", AbstractRustCodegen.CasingType.CAMEL_CASE, "p", "model", true);
Assert.assertTrue(listAppender.list.isEmpty());

codegen.sanitizeIdentifier("sanitize/warning/probe", AbstractRustCodegen.CasingType.CAMEL_CASE, "p", "model", true);
Assert.assertEquals(listAppender.list.size(), 1);
Assert.assertEquals(listAppender.list.get(0).getFormattedMessage(),
"SanitizeWarningProbe cannot be used as a model name. Renamed to SanitizeSlashWarningSlashProbe");
} finally {
logger.detachAppender(listAppender);
}
}

@Test
public void testToVarName() {
// Should be converted to snake case
Expand Down
Loading