Bug 1344590: Part 3 - Handle schema default values for object properties. r?aswan
MozReview-Commit-ID: 6rzMwvRl4ho
--- a/toolkit/components/extensions/Schemas.jsm
+++ b/toolkit/components/extensions/Schemas.jsm
@@ -1061,20 +1061,21 @@ class ObjectType extends Type {
// Only allow extending "properties" and "patternProperties".
extraProperties = ["additionalProperties", "isInstanceOf", ...extraProperties];
}
this.checkSchemaProperties(schema, path, extraProperties);
let parseProperty = (schema, extraProps = []) => {
return {
type: Schemas.parseSchema(schema, path,
- DEBUG && ["unsupported", "onError", "permissions", ...extraProps]),
+ DEBUG && ["unsupported", "onError", "permissions", "default", ...extraProps]),
optional: schema.optional || false,
unsupported: schema.unsupported || false,
onError: schema.onError || null,
+ default: schema.default === undefined ? null : schema.default,
};
};
// Parse explicit "properties" object.
let properties = Object.create(null);
for (let propName of Object.keys(schema.properties || {})) {
properties[propName] = parseProperty(schema.properties[propName], ["optional"]);
}
@@ -1184,42 +1185,42 @@ class ObjectType extends Type {
if (unsupported) {
if (prop in properties) {
error = context.error(`Property "${prop}" is unsupported by Firefox`,
`not contain an unsupported "${prop}" property`);
}
} else if (prop in properties) {
if (optional && (properties[prop] === null || properties[prop] === undefined)) {
- result[prop] = null;
+ result[prop] = propType.default;
} else {
let r = context.withPath(prop, () => type.normalize(properties[prop], context));
if (r.error) {
error = r;
} else {
result[prop] = r.value;
properties[prop] = r.value;
}
}
remainingProps.delete(prop);
} else if (!optional) {
error = context.error(`Property "${prop}" is required`,
`contain the required "${prop}" property`);
} else if (optional !== "omit-key-if-missing") {
- result[prop] = null;
+ result[prop] = propType.default;
}
if (error) {
if (onError == "warn") {
context.logError(error.error);
} else if (onError != "ignore") {
throw error;
}
- result[prop] = null;
+ result[prop] = propType.default;
}
}
normalize(value, context) {
try {
let v = this.normalizeBase("object", value, context);
if (v.error) {
return v;
--- a/toolkit/components/extensions/schemas/manifest.json
+++ b/toolkit/components/extensions/schemas/manifest.json
@@ -161,16 +161,17 @@
"type": "string",
"optional": true,
"format": "contentSecurityPolicy",
"onError": "warn"
},
"permissions": {
"type": "array",
+ "default": [],
"items": {
"choices": [
{ "$ref": "Permission" },
{
"type": "string",
"deprecated": "Unknown permission ${value}"
}
]