/// <summary> /// Comapres properties of the previous Schema to detect removed / added required properties /// </summary> /// <param name="context">Comaprision Context</param> /// <param name="priorSchema">Schema of the old model</param> private void CompareProperties(ComparisonContext <ServiceDefinition> context, Schema priorSchema) { // Case: Were any properties removed? if (priorSchema.Properties != null) { foreach (var def in priorSchema.Properties) { if (Properties == null || !Properties.TryGetValue(def.Key, out var model)) { context.LogBreakingChange(ComparisonMessages.RemovedProperty, def.Key); } else { context.PushProperty(def.Key); model.Compare(context, def.Value); context.Pop(); } } } // Case: Were any properties added? if (Properties != null) { foreach (KeyValuePair <string, Schema> property in Properties) { // Case: Were any required properties added? if ((priorSchema.Properties == null || !priorSchema.Properties.TryGetValue(property.Key, out var model)) && (Required != null && Required.Contains(property.Key))) { context.LogBreakingChange(ComparisonMessages.AddedRequiredProperty, property.Key); } // Case: Were any readOnly properties added in response direction? if (priorSchema.Properties != null && !priorSchema.Properties.TryGetValue(property.Key, out model)) { if (context.Direction == DataDirection.Response && property.Value != null) { if (property.Value.ReadOnly == true) { context.LogInfo(ComparisonMessages.AddedReadOnlyPropertyInResponse, property.Key); } else { context.LogBreakingChange(ComparisonMessages.AddedPropertyInResponse, property.Key); } } else if (priorSchema.IsReferenced && property.Value != null && (Required == null || !Required.Contains(property.Key))) { context.LogBreakingChange(ComparisonMessages.AddedOptionalProperty, property.Key); } } } } }
/// <summary> /// Comapres properties of the previous Schema to detect removed / added required properties /// </summary> /// <param name="context">Comaprision Context</param> /// <param name="priorSchema">Schema of the old model</param> private void CompareProperties(ComparisonContext context, Schema priorSchema) { // Case: Were any properties removed? if (priorSchema.Properties != null) { foreach (var def in priorSchema.Properties) { if (Properties == null || !Properties.TryGetValue(def.Key, out var model)) { context.LogBreakingChange(ComparisonMessages.RemovedProperty, def.Key); } else { context.PushProperty(def.Key); model.Compare(context, def.Value); context.Pop(); } } } // Case: Were any properties added? if (Properties != null) { foreach (var property in Properties) { // Case: Were any required properties added? Schema model = null; if (priorSchema.Properties == null || !priorSchema.Properties.TryGetValue(property.Key, out model) && Required != null && Required.Contains(property.Key)) { context.LogBreakingChange(ComparisonMessages.AddedRequiredProperty, property.Key); } // Case: Were any readOnly properties added in response direction? if (priorSchema.Properties != null && !priorSchema.Properties.TryGetValue(property.Key, out model)) { if (context.Direction == DataDirection.Response && property.Value != null) { if (property.Value.ReadOnly) { context.LogInfo(ComparisonMessages.AddedReadOnlyPropertyInResponse, property.Key); } else { context.LogBreakingChange(ComparisonMessages.AddedPropertyInResponse, property.Key); } } } } } }
public JsonSchema AddPropertyWithOverwrite(string propertyName, JsonSchema propertyDefinition, bool isRequired) { if (Properties != null && Properties.ContainsKey(propertyName)) { Properties.Remove(propertyName); } if (Required != null && Required.Contains(propertyName)) { Required.Remove(propertyName); } AddProperty(propertyName, propertyDefinition, isRequired); return(this); }
internal string Check() { var result = new StringBuilder(); foreach (var missingRequirement in Required.Where(req => !Provided.Contains(req))) { result.AppendLine("ERROR: " + missingRequirement.Source + " requires " + Context + " " + missingRequirement.Value + " but it's missing"); } if (CheckForAdditionals) { foreach (var additional in Provided.Where(req => !Required.Contains(req))) { result.AppendLine("WARN: " + additional.Source + " provides " + Context + " " + additional.Value + " but it's never used"); } } return(result.ToString()); }
/// <summary> /// Add the provided required property names to this JSON schema's list of required property names. /// </summary> /// <param name="requiredPropertyName"></param> /// <param name="extraRequiredPropertyNames"></param> public JsonSchema AddRequired(string requiredPropertyName, params string[] extraRequiredPropertyNames) { if (Properties == null || !Properties.ContainsKey(requiredPropertyName)) { throw new ArgumentException("No property exists with the provided requiredPropertyName (" + requiredPropertyName + ")", nameof(requiredPropertyName)); } if (Required == null) { Required = new List <string>(); } if (Required.Contains(requiredPropertyName)) { throw new ArgumentException("'" + requiredPropertyName + "' is already a required property.", "requiredPropertyName"); } Required.Add(requiredPropertyName); if (extraRequiredPropertyNames != null) { foreach (string extraRequiredPropertyName in extraRequiredPropertyNames) { if (Properties == null || !Properties.ContainsKey(extraRequiredPropertyName)) { throw new ArgumentException("No property exists with the provided extraRequiredPropertyName (" + extraRequiredPropertyName + ")", "extraRequiredPropertyNames"); } if (Required.Contains(extraRequiredPropertyName)) { throw new ArgumentException("'" + extraRequiredPropertyName + "' is already a required property.", "extraRequiredPropertyNames"); } Required.Add(extraRequiredPropertyName); } } return(this); }
private void CompareProperties(ComparisonContext context, Schema priorSchema) { // Were any properties removed? if (priorSchema.Properties != null) { foreach (var def in priorSchema.Properties) { Schema model = null; if (Properties == null || !Properties.TryGetValue(def.Key, out model)) { context.LogBreakingChange(ComparisonMessages.RemovedProperty1, def.Key); } else { context.PushProperty(def.Key); model.Compare(context, def.Value); context.Pop(); } } } // Were any required properties added? if (Properties != null) { foreach (var def in Properties.Keys) { Schema model = null; if (priorSchema.Properties == null || !priorSchema.Properties.TryGetValue(def, out model) && Required.Contains(def)) { context.LogBreakingChange(ComparisonMessages.AddedRequiredProperty1, def); } } } }
public bool IsRequired(string parameterName) { return(Required != null && Required.Contains(parameterName, StringComparer.OrdinalIgnoreCase)); }