diff --git a/src/object.js b/src/object.js index b9f977666..be56b7b95 100644 --- a/src/object.js +++ b/src/object.js @@ -7,6 +7,10 @@ var util = require("./util"); var Root; // cyclic +var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}; +var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"}; +var proto3Defaults = {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}; + /** * Constructs a new reflection object instance. * @classdesc Base class of all reflection objects. @@ -168,18 +172,30 @@ ReflectionObject.prototype.resolve = function resolve() { * @returns {undefined} */ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() { + var defaults = {}; + + if (this.root.getOption('syntax') === 'proto2') { + defaults = Object.assign({}, proto2Defaults); + } else if (this.root.getOption('syntax') === 'proto3') { + defaults = Object.assign({}, proto3Defaults) + } else if (this.root.getOption('edition') === '2023') { + defaults = Object.assign({}, editions2023Defaults); + } + if (this.parent) { // This is an annoying workaround since we can't use the spread operator // (Breaks the bundler and eslint) // If we don't create a shallow copy, we end up also altering the parent's // features - var parentFeatures = Object.assign({}, this.parent._proto_features); - this._features = Object.assign(parentFeatures, this._proto_features || {}); + var parentFeaturesMerged = Object.assign(defaults, this.parent._proto_features); + this._features = Object.assign(parentFeaturesMerged, this._proto_features || {}); + this._proto_features = this._features; this.parent._resolveFeatures(); } else { - this._features = Object.assign({}, this._proto_features); + this._features = Object.assign(defaults, this._proto_features || {}); } this._proto_features = this._features; + }; /** diff --git a/src/parse.js b/src/parse.js index 0c661de11..8135aa2f0 100644 --- a/src/parse.js +++ b/src/parse.js @@ -27,10 +27,6 @@ var base10Re = /^[1-9][0-9]*$/, nameRe = /^[a-zA-Z_][a-zA-Z_0-9]*$/, typeRefRe = /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/; -var editions2023Defaults = {features: {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}}; -var proto2Defaults = {features: {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"}}; -var proto3Defaults = {features: {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"}}; - /** * Result object returned from {@link parse}. * @interface IParserResult @@ -278,16 +274,6 @@ function parse(source, root, options) { // Otherwise the meaning is ambiguous between proto2 and proto3 root.setOption("syntax", syntax); - if (isProto3) { - for (var proto3Key of Object.keys(proto3Defaults)) { - setParsedOption(root, proto3Key, proto3Defaults[proto3Key]); - } - } else { - for (var proto2Key of Object.keys(proto2Defaults)) { - setParsedOption(root, proto2Key, proto2Defaults[proto2Key]); - } - } - skip(";"); } @@ -302,9 +288,6 @@ function parse(source, root, options) { root.setOption("edition", edition); - for (var key of Object.keys(editions2023Defaults)) { - setParsedOption(root, key, editions2023Defaults[key]); - } skip(";"); } diff --git a/tests/comp_options.js b/tests/comp_options.js index a95e7c25f..ec9888482 100644 --- a/tests/comp_options.js +++ b/tests/comp_options.js @@ -30,7 +30,7 @@ message Message { tape.test("complex options", function (test) { var root = protobuf.parse(proto).root; - test.deepEqual(root.parsedOptions[1], { + test.deepEqual(root.parsedOptions[0], { "(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger)": { info: { title: "Some info", diff --git a/tests/feature_resolution_editions.js b/tests/feature_resolution_editions.js index 5c8493dbd..cf123e5bc 100644 --- a/tests/feature_resolution_editions.js +++ b/tests/feature_resolution_editions.js @@ -193,7 +193,7 @@ service MyService { }; } ` -tape.test("feautre resolution defaults", function(test) { +tape.test("feature resolution defaults", function(test) { var rootEditions = protobuf.parse(protoEditions2023).root; rootEditions.resolveAll(); test.same(rootEditions._features, editions2023Defaults);