Skip to content

Commit

Permalink
fix: special characters in java enum values (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
quadrrem authored Dec 14, 2021
1 parent 563af19 commit 615eb60
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generators/java/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ${this.indent(this.renderBlock(content, 2))}
break;
}
default: {
key = String(value);
key = FormatHelpers.replaceSpecialCharacters(String(value), { exclude: [' '], separator: '_' });
//Ensure no special char can be the beginning letter
if (!(/^[a-zA-Z]+$/).test(key.charAt(0))) {
key = `string_${key}`;
Expand Down
48 changes: 48 additions & 0 deletions test/generators/java/JavaGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,54 @@ public enum CustomEnum {
expect(enumModel.dependencies).toEqual(expectedDependencies);
});

test('should render enums with translated special characters', async () => {
const doc = {
$id: 'States',
enum: ['test+', 'test', 'test-', 'test?!', '*test']
};
const expected = `public enum States {
TEST_PLUS("test+"), TEST("test"), TEST_MINUS("test-"), TEST_QUESTION_EXCLAMATION("test?!"), ASTERISK_TEST("*test");
private String value;
States(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static States fromValue(String value) {
for (States e : States.values()) {
if (e.value.equals(value)) {
return e;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}`;

const inputModel = await generator.process(doc);
const model = inputModel.models['States'];

let enumModel = await generator.renderEnum(model, inputModel);
const expectedDependencies = ['import com.fasterxml.jackson.annotation.*;'];
expect(enumModel.result).toEqual(expected);
expect(enumModel.dependencies).toEqual(expectedDependencies);

enumModel = await generator.render(model, inputModel);
expect(enumModel.result).toEqual(expected);
expect(enumModel.dependencies).toEqual(expectedDependencies);
});

test('should render List type for collections', async () => {
const doc = {
$id: 'CustomClass',
Expand Down

0 comments on commit 615eb60

Please sign in to comment.