Skip to content
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

fix: add more tests and fix recursion handling #77

Merged
merged 9 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ async function validate(input: ValidateSchemaInput<unknown, unknown>): Promise<S
message = error.message;
}

const validateResult: SchemaValidateResult[] = [{
return [{
message,
path: input.path, // protobuff parser doesn't provide a path to the error.
path: input.path, // protobuf parser doesn't provide a path to the error.
}];

return validateResult;
}
}

Expand Down
56 changes: 36 additions & 20 deletions src/protoj2jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,6 @@ class Proto2JsonSchema {
if (!weak) {
throw new Error('Imports are currently not implemented');
}
/*
// Otherwise fetch from disk or network
let source: string;
try {
source = util.fs.readFileSync(filename).toString('utf8');
} catch (err) {
if (!weak) {
throw err;
}

return;
}
this.process(filename, source);
*/
}

public compile(): AsyncAPISchema {
Expand All @@ -115,7 +101,7 @@ class Proto2JsonSchema {
const rootItemCandidates = this.resolveByFilename(ROOT_FILENAME, this.root.nested as ProtoItems);
const rootItem = this.findRootItem(rootItemCandidates);

return this.compileMessage(rootItem);
return this.compileMessage(rootItem, []);
}

private resolveByFilename(filename: string, items: ProtoItems) {
Expand Down Expand Up @@ -186,16 +172,29 @@ class Proto2JsonSchema {
* Compiles a protobuf message to JSON schema
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
private compileMessage(item: protobuf.Type): AsyncAPISchemaDefinition {
private compileMessage(item: protobuf.Type, stack: string[]): AsyncAPISchemaDefinition {
const obj: v3.AsyncAPISchemaDefinition = {
title: item.name,
type: 'object',
required: [],
properties: {},
};

const timesSeenThisClassInStack = stack.filter(x => x === item.name).length;
if (timesSeenThisClassInStack >= 2) {
// Detected a recursion.
return obj;
}

stack.push(item.name);

for (const fieldName in item.fields) {
const field = item.fields[fieldName];

if (field.partOf) {
continue;
}

if (field.required) {
obj.required?.push(fieldName);
}
Expand All @@ -205,7 +204,7 @@ class Proto2JsonSchema {
obj.properties![field.name] = {
description: this.extractDescription(field.comment) || '',
type: 'array',
items: this.compileField(field, item),
items: this.compileField(field, item, stack.slice()),
};

if (field.comment) {
Expand All @@ -221,7 +220,24 @@ class Proto2JsonSchema {
}
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
obj.properties![field.name] = this.compileField(field, item);
obj.properties![field.name] = this.compileField(field, item, stack.slice());
}
}

for (const oneOff of item.oneofsArray) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (!obj.properties![oneOff.name]) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
obj.properties![oneOff.name] = {
oneOf: []
};
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const oneOf = (obj.properties![oneOff.name] as any)['oneOf'] as any[];

for (const fieldName of oneOff.oneof) {
const field = item.fields[fieldName];
oneOf.push(this.compileField(field, item, stack.slice()));
}
}

Expand Down Expand Up @@ -254,7 +270,7 @@ class Proto2JsonSchema {
return obj;
}

private compileField(field: protobuf.Field, parentItem: protobuf.Type): v3.AsyncAPISchemaDefinition {
private compileField(field: protobuf.Field, parentItem: protobuf.Type, stack: string[]): v3.AsyncAPISchemaDefinition {
let obj: v3.AsyncAPISchemaDefinition = {};

if (PrimitiveTypes.PRIMITIVE_TYPES[field.type.toLowerCase()]) {
Expand All @@ -270,7 +286,7 @@ class Proto2JsonSchema {
if (item instanceof protobuf.Enum) {
obj = Object.assign(obj, this.compileEnum(item));
} else {
obj = Object.assign(obj, this.compileMessage(item));
obj = Object.assign(obj, this.compileMessage(item, stack));
}
}

Expand Down
72 changes: 23 additions & 49 deletions test/documents/comments.proto.result.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,19 @@
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"y": {
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"label": {
"type": "string",
"x-primitive": "string",

"x-primitive": "string"
}
},

}
},
"end": {
"title": "Point",
Expand All @@ -59,34 +55,26 @@
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"y": {
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"label": {
"type": "string",
"x-primitive": "string",

"x-primitive": "string"
}
},

}
},
"label": {
"type": "string",
"x-primitive": "string",

"x-primitive": "string"
}
},

},


}
}
}
}
}
Expand Down Expand Up @@ -115,23 +103,19 @@
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"y": {
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"label": {
"type": "string",
"x-primitive": "string",

"x-primitive": "string"
}
},

}
},
"end": {
"title": "Point",
Expand All @@ -145,37 +129,27 @@
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"y": {
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"x-primitive": "int32",

"x-primitive": "int32"
},
"label": {
"type": "string",
"x-primitive": "string",

"x-primitive": "string"
}
},

}
},
"label": {
"type": "string",
"x-primitive": "string",

"x-primitive": "string"
}
},

},


}
}
}
}
},


}
}
Loading
Loading