Skip to content

Commit

Permalink
[core] Don't reject schemas with issues, but add valid properties.
Browse files Browse the repository at this point in the history
Improves #4902

Signed-off-by: Sven Efftinge <[email protected]>
  • Loading branch information
svenefftinge committed Oct 7, 2019
1 parent 0f7fc56 commit b4972e3
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions packages/core/src/browser/preferences/preference-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,42 +173,64 @@ export class PreferenceSchemaProvider extends PreferenceProvider {
const valid = ajv.validateSchema(schema);
if (!valid) {
const errors = !!ajv.errors ? ajv.errorsText(ajv.errors) : 'unknown validation error';
console.error('An invalid preference schema was rejected: ' + errors);
return [];
console.warn('A preference schema with validation issues was registered: ' + errors);
}
const changes: PreferenceProviderDataChange[] = [];
const scope = PreferenceScope.Default;
const domain = this.getDomain();
const changes: PreferenceProviderDataChange[] = [];
const defaultScope = PreferenceSchema.getDefaultScope(schema);
const overridable = schema.overridable || false;
for (const preferenceName of Object.keys(schema.properties)) {
if (this.combinedSchema.properties[preferenceName]) {
console.error('Preference name collision detected in the schema for property: ' + preferenceName);
} else {
const schemaProps = PreferenceDataProperty.fromPreferenceSchemaProperty(schema.properties[preferenceName], defaultScope);
if (typeof schemaProps.overridable !== 'boolean' && overridable) {
schemaProps.overridable = true;
}
if (schemaProps.overridable) {
this.overridePatternProperties.properties[preferenceName] = schemaProps;
const cleanedProps = this.cleanProperties(schema.properties, defaultScope, overridable);
for (const propertyName of Object.keys(cleanedProps)) {
const schemaProp = cleanedProps[propertyName];
this.combinedSchema.properties[propertyName] = <PreferenceDataProperty>schemaProp;
this.unsupportedPreferences.delete(propertyName);

const value = schemaProp.defaultValue = this.getDefaultValue(schemaProp, propertyName);
if (this.testOverrideValue(propertyName, value)) {
for (const overriddenPreferenceName in value) {
const overrideValue = value[overriddenPreferenceName];
const overridePreferenceName = `${propertyName}.${overriddenPreferenceName}`;
changes.push(this.doSetPreferenceValue(overridePreferenceName, overrideValue, { scope, domain }));
}
this.combinedSchema.properties[preferenceName] = schemaProps;
this.unsupportedPreferences.delete(preferenceName);

const value = schemaProps.defaultValue = this.getDefaultValue(schemaProps, preferenceName);
if (this.testOverrideValue(preferenceName, value)) {
for (const overriddenPreferenceName in value) {
const overrideValue = value[overriddenPreferenceName];
const overridePreferenceName = `${preferenceName}.${overriddenPreferenceName}`;
changes.push(this.doSetPreferenceValue(overridePreferenceName, overrideValue, { scope, domain }));
}
} else {
changes.push(this.doSetPreferenceValue(propertyName, value, { scope, domain }));
}
}
return changes;
}

protected cleanProperties(properties: { [key: string]: PreferenceSchemaProperty | PreferenceDataProperty },
defaultScope: PreferenceScope, overrideable: boolean, prefix?: string): { [key: string]: PreferenceDataProperty } {
for (const preferenceName of Object.keys(properties)) {
const longName = (prefix ? prefix + '.' : '') + preferenceName;
if (this.combinedSchema.properties[longName]) {
console.error('Preference name collision detected in the schema for property: ' + longName);
delete properties[preferenceName];
} else {
const property = properties[preferenceName];
if (!PreferenceSchemaProperties.is(property)) {
console.warn(`Removing invalid property ${preferenceName}`);
delete properties[preferenceName];
} else {
changes.push(this.doSetPreferenceValue(preferenceName, value, { scope, domain }));
const schemaProps = PreferenceDataProperty.fromPreferenceSchemaProperty(property, defaultScope);
if (typeof schemaProps.overridable !== 'boolean' && overrideable) {
schemaProps.overridable = true;
}
if (schemaProps.overridable) {
this.overridePatternProperties.properties[preferenceName] = schemaProps;
}
const nested = schemaProps.properties || schemaProps.items && schemaProps.items.properties;
if (nested) {
this.cleanProperties(nested, defaultScope, overrideable, longName);
}
properties[preferenceName] = schemaProps;
}
}
}
return changes;
return properties as { [k: string]: PreferenceDataProperty };
}

protected doSetPreferenceValue(preferenceName: string, newValue: any, { scope, domain }: {
scope: PreferenceScope,
domain?: string[]
Expand Down

0 comments on commit b4972e3

Please sign in to comment.