Skip to content

fix: app does not sanitize invalid identifiers #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 30 additions & 2 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -130,8 +130,9 @@ class Analyzer {
let index = 1;

for (const [key, value] of Object.entries(json)) {
let sanitizeKey = this.sanitizeKey(key);
// It is assumed that the key is either in camelCase or snake_case (see https://jsonapi.org/recommendations/).
const formattedKey = this.options.lowerSnakeCaseFieldNames ? key.replace(/([A-Z])/g, "_$1").toLowerCase() : key;
const formattedKey = this.options.lowerSnakeCaseFieldNames ? sanitizeKey.replace(/([A-Z])/g, "_$1").toLowerCase() : sanitizeKey;
const typeName = this.analyzeProperty(formattedKey, value, collector, inlineShift)

lines.push(` ${typeName} ${formattedKey} = ${index};`);
@@ -142,6 +143,32 @@ class Analyzer {
return render(collector.getImports(), collector.getMessages(), lines, this.options);
}

sanitizeKey(key: string): string {
const keyArray = key.split("");
let result = "";
for (const { index, char } of keyArray.map((char, index) => ({
char,
index,
}))) {
let newChar = char;
if (index === 0 && !char.match(/[a-zA-Z]/)) {
throw new Error(
`Invalid character: '${char}'. The first character must be a letter. Invalid key: "${key}".`
);
} else if (char === "-") {
newChar = "_";
} else if (char.match(/[a-zA-Z0-9]/)) newChar = char;
else
throw new Error(
`Invalid character in position ${
index + 1
}. The character "${char}" is not allowed. Invalid key: "${key}".`
);
result += newChar;
}
return result;
}

analyzeArrayProperty(key: string, value: Array<any>, collector: Collector, inlineShift: string): string {
// [] -> any
const length = value.length;
@@ -323,8 +350,9 @@ class Analyzer {
let index = 1;

for (const [key, value] of Object.entries(source)) {
const sanitizeKey = this.sanitizeKey(key);
// It is assumed that the key is either in camelCase or snake_case (see https://jsonapi.org/recommendations/).
const formattedKey = this.options.lowerSnakeCaseFieldNames ? key.replace(/([A-Z])/g, "_$1").toLowerCase() : key;
const formattedKey = this.options.lowerSnakeCaseFieldNames ? sanitizeKey.replace(/([A-Z])/g, "_$1").toLowerCase() : sanitizeKey;
const typeName = this.analyzeProperty(formattedKey, value, collector, inlineShift)

lines.push(`${inlineShift} ${typeName} ${formattedKey} = ${index};`);
36 changes: 36 additions & 0 deletions src/tests/convert.ts
Original file line number Diff line number Diff line change
@@ -335,5 +335,41 @@ message SomeMessage {
}
}

{
let json = `{
"FirstName": "Hanna",
"w12-of": 43.21
}`;

assert(json, `syntax = "proto3";

message SomeMessage {
string FirstName = 1;
double w12_of = 2;
}`);
}

t.end();
});

test("convert test exceptions", (t) => {
const options = new Options(true, false, true, false);
function assert(json: string, message: string, overrideOptions?: Options) {
t.equal(convert(json, overrideOptions ?? options).error, message);
}

{
let json = `{"1": "hello", "2": "world"}`;
assert(json, `Invalid character: '1'. The first character must be a letter. Invalid key: "1".`);
}

{
let json = `{
"FirstName": "Hanna",
"12-of": 43.21
}`;
assert(json, `Invalid character: '1'. The first character must be a letter. Invalid key: "12-of".`);
}

t.end();
});
2 changes: 1 addition & 1 deletion static/js/app.js

Large diffs are not rendered by default.