Skip to content

Commit

Permalink
fix: escape java string literals used for regex patterns and string c…
Browse files Browse the repository at this point in the history
…onstants (#1509)

Co-authored-by: Daniel Kenyon-Jones <kenyonjd@featurespace.co.uk>
  • Loading branch information
dlkj and danielkenyonjonesfs authored Oct 9, 2023
1 parent f027c20 commit e4c1bd9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/generators/java/JavaRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ ${newLiteral}
}
return values !== undefined ? `${name}(${values})` : name;
}

static renderStringLiteral(value: string): string {
const escaped = value
.replace(/\\/g, '\\\\')
.replace(/\t/g, '\\t')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\f/g, '\\f')
.replace(/"/g, '\\"');
return `"${escaped}"`;
}

renderStringLiteral(value: string): string {
return JavaRenderer.renderStringLiteral(value);
}
}
3 changes: 2 additions & 1 deletion src/generators/java/constrainer/ConstantConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ConstrainedStringModel
} from '../../../models';
import { ConstantConstraint } from '../../../helpers';
import { JavaRenderer } from '../JavaRenderer';

const getConstrainedEnumModelConstant = (args: {
constrainedMetaModel: ConstrainedMetaModel;
Expand Down Expand Up @@ -45,7 +46,7 @@ export function defaultConstantConstraints(): ConstantConstraint {
constOptions
});
} else if (constrainedMetaModel instanceof ConstrainedStringModel) {
return `"${constOptions.originalInput}"`;
return JavaRenderer.renderStringLiteral(`${constOptions.originalInput}`);
}

return undefined;
Expand Down
4 changes: 3 additions & 1 deletion src/generators/java/presets/ConstraintsPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export const JAVA_CONSTRAINTS_PRESET: JavaPreset = {
const pattern = originalInput['pattern'];
if (pattern !== undefined) {
annotations.push(
renderer.renderAnnotation('Pattern', { regexp: `"${pattern}"` })
renderer.renderAnnotation('Pattern', {
regexp: renderer.renderStringLiteral(pattern)
})
);
}
const minLength = originalInput['minLength'];
Expand Down
12 changes: 12 additions & 0 deletions test/generators/java/JavaRenderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,16 @@ describe('JavaRenderer', () => {
).toEqual('@SomeComment(test=test2)');
});
});

describe('renderStringLiteral()', () => {
test('Should be able to render string literal with special characters', () => {
expect(
renderer.renderStringLiteral(
'this is a "literal" string with \'special\' characters: \\a\n\t\\b'
)
).toEqual(
'"this is a \\"literal\\" string with \'special\' characters: \\\\a\\n\\t\\\\b"'
);
});
});
});
6 changes: 5 additions & 1 deletion test/generators/java/presets/ConstraintsPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ describe('JAVA_CONSTRAINTS_PRESET', () => {
min_number_prop: { type: 'number', minimum: 0 },
max_number_prop: { type: 'number', exclusiveMaximum: 100 },
array_prop: { type: 'array', minItems: 2, maxItems: 3 },
string_prop: { type: 'string', pattern: '^I_', minLength: 3 }
string_prop: {
type: 'string',
pattern: '^\\w+("\\.\\w+)*$',
minLength: 3
}
},
required: ['min_number_prop', 'max_number_prop']
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render constraints annotations 1`] = `
private double maxNumberProp;
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^I_\\")
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")
@Size(min=3)
private String stringProp;
private Map<String, Object> additionalProperties;
Expand Down

0 comments on commit e4c1bd9

Please sign in to comment.