diff --git a/src/converter.js b/src/converter.js index ac67c3d72..676514bc2 100644 --- a/src/converter.js +++ b/src/converter.js @@ -193,7 +193,7 @@ function genValuePartial_toObject(gen, field, fieldIndex, prop) { */ converter.toObject = function toObject(mtype) { /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */ - var fields = mtype.fieldsArray; + var fields = mtype.fieldsArray.sort(util.compareFieldsById); if (!fields.length) return util.codegen()("return {}"); var gen = util.codegen("m", "o") @@ -265,6 +265,9 @@ converter.toObject = function toObject(mtype) { ("}"); } else genValuePartial_toObject(gen, field, i, prop); + if (field.partOf) gen + ("if(o.oneofs)") + ("d%s=%j", util.safeProp(field.partOf.name), field.name); gen ("}"); } diff --git a/src/type.js b/src/type.js index 65dca4a8c..b38f3f387 100644 --- a/src/type.js +++ b/src/type.js @@ -484,6 +484,7 @@ Type.prototype.from = Type.prototype.fromObject; * @property {boolean} [defaults=false] Also sets default values on the resulting object * @property {boolean} [arrays=false] Sets empty arrays for missing repeated fields even if `defaults=false` * @property {boolean} [objects=false] Sets empty objects for missing map fields even if `defaults=false` + * @property {boolean} [oneofs=false] Includes virtual oneof properties set to the present field's name, if any */ /**