private void CompareEnums(ComparisonContext context, SwaggerObject prior) { if (prior.Enum == null && Enum == null) { return; } var relaxes = prior.Enum != null && Enum == null; var constrains = prior.Enum == null && Enum != null; if (!relaxes && !constrains) { // It was enum and it is still enum i.e check for addition/removal // 1. Look for removed elements (constraining). constrains = prior.Enum.Any(str => !Enum.Contains(str)); // 2. Look for added elements (relaxing). relaxes = Enum.Any(str => !prior.Enum.Contains(str)); } if (context.Direction == DataDirection.Request) { if (constrains) { var removedEnums = prior.Enum.Except(Enum); if (removedEnums.Any()) { context.LogBreakingChange(ComparisonMessages.RemovedEnumValue, string.Join(", ", removedEnums.ToList())); } return; } } else if (context.Direction == DataDirection.Response) { if (relaxes) { var addedEnums = Enum.Except(prior.Enum); if (addedEnums.Any()) { context.LogBreakingChange(ComparisonMessages.AddedEnumValue, string.Join(", ", addedEnums.ToList())); } return; } } if (relaxes && constrains) { context.LogInfo(ComparisonMessages.ConstraintChanged, "enum"); } else if (relaxes || constrains) { context.LogInfo( relaxes ? ComparisonMessages.ConstraintIsWeaker : ComparisonMessages.ConstraintIsStronger, "enum"); } }
public static void PopulateParameter(IParameter parameter, SwaggerObject swaggerObject) { if (swaggerObject == null) { throw new ArgumentNullException("swaggerObject"); } if (parameter == null) { throw new ArgumentNullException("parameter"); } parameter.IsRequired = swaggerObject.IsRequired; parameter.DefaultValue = swaggerObject.Default; if (IsSwaggerObjectConstant(swaggerObject)) { parameter.DefaultValue = swaggerObject.Enum[0]; parameter.IsConstant = true; } var compositeType = parameter.Type as CompositeType; if (compositeType != null && compositeType.ComposedProperties.Any()) { if (compositeType.ComposedProperties.All(p => p.IsConstant)) { parameter.DefaultValue = "{}"; parameter.IsConstant = true; } } parameter.Documentation = swaggerObject.Description; parameter.CollectionFormat = swaggerObject.CollectionFormat; var enumType = parameter.Type as EnumType; if (enumType != null) { if (parameter.Documentation == null) { parameter.Documentation = string.Empty; } else { parameter.Documentation = parameter.Documentation.TrimEnd('.') + ". "; } parameter.Documentation += "Possible values include: " + string.Join(", ", enumType.Values.Select(v => string.Format(CultureInfo.InvariantCulture, "'{0}'", v.Name))); } swaggerObject.Extensions.ForEach(e => parameter.Extensions[e.Key] = e.Value); SetConstraints(parameter.Constraints, swaggerObject); }
private void CompareEnums(ComparisonContext context, SwaggerObject prior) { if (prior.Enum == null && this.Enum == null) { return; } bool relaxes = (prior.Enum != null && this.Enum == null); bool constrains = (prior.Enum == null && this.Enum != null); if (!relaxes && !constrains) { // 1. Look for removed elements (constraining). constrains = prior.Enum.Any(str => !this.Enum.Contains(str)); // 2. Look for added elements (relaxing). relaxes = this.Enum.Any(str => !prior.Enum.Contains(str)); } if (context.Direction == DataDirection.Request) { if (constrains) { context.LogBreakingChange(relaxes ? ComparisonMessages.ConstraintChanged : ComparisonMessages.ConstraintIsStronger, "enum"); return; } } else if (context.Direction == DataDirection.Response) { if (relaxes) { context.LogBreakingChange(constrains ? ComparisonMessages.ConstraintChanged : ComparisonMessages.ConstraintIsWeaker, "enum"); return; } } if (relaxes && constrains) { context.LogInfo(ComparisonMessages.ConstraintChanged, "enum"); } else if (relaxes || constrains) { context.LogInfo(relaxes ? ComparisonMessages.ConstraintIsWeaker : ComparisonMessages.ConstraintIsStronger, "enum"); } }
protected void CompareItems(ComparisonContext context, SwaggerObject prior) { if (prior == null) { throw new ArgumentNullException("prior"); } if (context == null) { throw new ArgumentNullException("context"); } if (prior.Items != null && Items != null) { context.Push("items"); Items.Compare(context, prior.Items); context.Pop(); } }
protected void CompareFormats(ComparisonContext context, SwaggerObject prior) { if (prior == null) { throw new ArgumentNullException("prior"); } if (context == null) { throw new ArgumentNullException("context"); } if (prior.Format == null && Format != null || prior.Format != null && Format == null || prior.Format != null && Format != null && !prior.Format.Equals(Format)) { context.LogBreakingChange(ComparisonMessages.TypeFormatChanged); } }
private void CompareProperties(ComparisonContext context, SwaggerObject prior) { // Additional properties if (prior.AdditionalProperties == null && AdditionalProperties != null) { context.LogBreakingChange(ComparisonMessages.AddedAdditionalProperties); } else if (prior.AdditionalProperties != null && AdditionalProperties == null) { context.LogBreakingChange(ComparisonMessages.RemovedAdditionalProperties); } else if (AdditionalProperties != null) { context.Push("additionalProperties"); AdditionalProperties.Compare(context, prior.AdditionalProperties); context.Pop(); } }
private static bool IsSwaggerObjectConstant(SwaggerObject swaggerObject) { return (swaggerObject.Enum != null && swaggerObject.Enum.Count == 1 && swaggerObject.IsRequired); }
public ObjectBuilder(SwaggerObject swaggerObject, SwaggerModeler modeler) { SwaggerObject = swaggerObject; Modeler = modeler; }
public static void SetConstraints(Dictionary<Constraint, string> constraints, SwaggerObject swaggerObject) { if (constraints == null) { throw new ArgumentNullException("constraints"); } if (swaggerObject == null) { throw new ArgumentNullException("swaggerObject"); } if (!string.IsNullOrEmpty(swaggerObject.Maximum) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.Maximum)) && !swaggerObject.ExclusiveMaximum) { constraints[Constraint.InclusiveMaximum] = swaggerObject.Maximum; } if (!string.IsNullOrEmpty(swaggerObject.Maximum) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.Maximum)) && swaggerObject.ExclusiveMaximum && swaggerObject.IsConstraintSupported(nameof(swaggerObject.ExclusiveMaximum))) { constraints[Constraint.ExclusiveMaximum] = swaggerObject.Maximum; } if (!string.IsNullOrEmpty(swaggerObject.Minimum) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.Minimum)) && !swaggerObject.ExclusiveMinimum) { constraints[Constraint.InclusiveMinimum] = swaggerObject.Minimum; } if (!string.IsNullOrEmpty(swaggerObject.Minimum) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.Minimum)) && swaggerObject.ExclusiveMinimum && swaggerObject.IsConstraintSupported(nameof(swaggerObject.ExclusiveMinimum))) { constraints[Constraint.ExclusiveMinimum] = swaggerObject.Minimum; } if (!string.IsNullOrEmpty(swaggerObject.MaxLength) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.MaxLength))) { constraints[Constraint.MaxLength] = swaggerObject.MaxLength; } if (!string.IsNullOrEmpty(swaggerObject.MinLength) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.MinLength))) { constraints[Constraint.MinLength] = swaggerObject.MinLength; } if (!string.IsNullOrEmpty(swaggerObject.Pattern) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.Pattern))) { constraints[Constraint.Pattern] = swaggerObject.Pattern; } if (!string.IsNullOrEmpty(swaggerObject.MaxItems) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.MaxItems))) { constraints[Constraint.MaxItems] = swaggerObject.MaxItems; } if (!string.IsNullOrEmpty(swaggerObject.MinItems) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.MinItems))) { constraints[Constraint.MinItems] = swaggerObject.MinItems; } if (!string.IsNullOrEmpty(swaggerObject.MultipleOf) && swaggerObject.IsConstraintSupported(nameof(swaggerObject.MultipleOf))) { constraints[Constraint.MultipleOf] = swaggerObject.MultipleOf; } if (swaggerObject.UniqueItems && swaggerObject.IsConstraintSupported(nameof(swaggerObject.UniqueItems))) { constraints[Constraint.UniqueItems] = "true"; } }
protected void CompareConstraints(ComparisonContext context, SwaggerObject prior) { if (prior == null) { throw new ArgumentNullException("prior"); } if (context == null) { throw new ArgumentNullException("context"); } if ((prior.MultipleOf == null && MultipleOf != null) || (prior.MultipleOf != null && !prior.MultipleOf.Equals(MultipleOf))) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "multipleOf"); } if ((prior.Maximum == null && Maximum != null) || (prior.Maximum != null && !prior.Maximum.Equals(Maximum)) || prior.ExclusiveMaximum != ExclusiveMaximum) { // Flag stricter constraints for requests and relaxed constraints for responses. if (prior.ExclusiveMaximum != ExclusiveMaximum || context.Direction == DataDirection.None) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "maximum"); } else if (context.Direction == DataDirection.Request && Narrows(prior.Maximum, Maximum, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "maximum"); } else if (context.Direction == DataDirection.Response && Widens(prior.Maximum, Maximum, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "maximum"); } else if (Narrows(prior.Maximum, Maximum, false)) { context.LogInfo(ComparisonMessages.ConstraintIsStronger, "maximum"); } else if (Widens(prior.Maximum, Maximum, false)) { context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "maximum"); } } if ((prior.Minimum == null && Minimum != null) || (prior.Minimum != null && !prior.Minimum.Equals(Minimum)) || prior.ExclusiveMinimum != ExclusiveMinimum) { // Flag stricter constraints for requests and relaxed constraints for responses. if (prior.ExclusiveMinimum != ExclusiveMinimum || context.Direction == DataDirection.None) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "minimum"); } else if (context.Direction == DataDirection.Request && Narrows(prior.Minimum, Minimum, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "minimum"); } else if (context.Direction == DataDirection.Response && Widens(prior.Minimum, Minimum, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "minimum"); } else if (Narrows(prior.Minimum, Minimum, false)) { context.LogInfo(ComparisonMessages.ConstraintIsStronger, "minimum"); } else if (Widens(prior.Minimum, Minimum, false)) { context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "minimum"); } } if ((prior.MaxLength == null && MaxLength != null) || (prior.MaxLength != null && !prior.MaxLength.Equals(MaxLength))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "maxLength"); } else if (context.Direction == DataDirection.Request && Narrows(prior.MaxLength, MaxLength, false)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "maxLength"); } else if (context.Direction == DataDirection.Response && Widens(prior.MaxLength, MaxLength, false)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "maxLength"); } else if (Narrows(prior.MaxLength, MaxLength, false)) { context.LogInfo(ComparisonMessages.ConstraintIsStronger, "maxLength"); } else if (Widens(prior.MaxLength, MaxLength, false)) { context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "maxLength"); } } if ((prior.MinLength == null && MinLength != null) || (prior.MinLength != null && !prior.MinLength.Equals(MinLength))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "minLength"); } else if (context.Direction == DataDirection.Request && Narrows(prior.MinLength, MinLength, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "minimum"); } else if (context.Direction == DataDirection.Response && Widens(prior.MinLength, MinLength, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "minimum"); } else if (Narrows(prior.MinLength, MinLength, false)) { context.LogInfo(ComparisonMessages.ConstraintIsStronger, "minLength"); } else if (Widens(prior.MinLength, MinLength, false)) { context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "minLength"); } } if ((prior.Pattern == null && Pattern != null) || (prior.Pattern != null && !prior.Pattern.Equals(Pattern))) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "pattern"); } if ((prior.MaxItems == null && MaxItems != null) || (prior.MaxItems != null && !prior.MaxItems.Equals(MaxItems))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "maxItems"); } else if (context.Direction == DataDirection.Request && Narrows(prior.MaxItems, MaxItems, false)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "maxItems"); } else if (context.Direction == DataDirection.Response && Widens(prior.MaxItems, MaxItems, false)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "maxItems"); } else if (Narrows(prior.MaxItems, MaxItems, false)) { context.LogInfo(ComparisonMessages.ConstraintIsStronger, "maxItems"); } else if (Widens(prior.MaxItems, MaxItems, false)) { context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "maxItems"); } } if ((prior.MinItems == null && MinItems != null) || (prior.MinItems != null && !prior.MinItems.Equals(MinItems))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "minItems"); } else if (context.Direction == DataDirection.Request && Narrows(prior.MinItems, MinItems, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "minItems"); } else if (context.Direction == DataDirection.Response && Widens(prior.MinItems, MinItems, true)) { context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "minItems"); } else if (Narrows(prior.MinItems, MinItems, true)) { context.LogInfo(ComparisonMessages.ConstraintIsStronger, "minItems"); } else if (Widens(prior.MinItems, MinItems, true)) { context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "minItems"); } } if (prior.UniqueItems != UniqueItems) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "uniqueItems"); } }
private void CompareEnums(ComparisonContext context, SwaggerObject prior) { if (prior.Enum == null && this.Enum == null) return; bool relaxes = (prior.Enum != null && this.Enum == null); bool constrains = (prior.Enum == null && this.Enum != null); if (!relaxes && !constrains) { // 1. Look for removed elements (constraining). constrains = prior.Enum.Any(str => !this.Enum.Contains(str)); // 2. Look for added elements (relaxing). relaxes = this.Enum.Any(str => !prior.Enum.Contains(str)); } if (context.Direction == DataDirection.Request) { if (constrains) { context.LogBreakingChange(relaxes ? ComparisonMessages.ConstraintChanged : ComparisonMessages.ConstraintIsStronger, "enum"); return; } } else if (context.Direction == DataDirection.Response) { if (relaxes) { context.LogBreakingChange(constrains ? ComparisonMessages.ConstraintChanged : ComparisonMessages.ConstraintIsWeaker, "enum"); return; } } if (relaxes && constrains) context.LogInfo(ComparisonMessages.ConstraintChanged, "enum"); else if (relaxes || constrains) context.LogInfo(relaxes ? ComparisonMessages.ConstraintIsWeaker : ComparisonMessages.ConstraintIsStronger, "enum"); }
protected void CompareConstraints(ComparisonContext context, SwaggerObject prior) { if (prior == null) { throw new ArgumentNullException("prior"); } if (context == null) { throw new ArgumentNullException("context"); } if ((prior.MultipleOf == null && MultipleOf != null) || (prior.MultipleOf != null && !prior.MultipleOf.Equals(MultipleOf))) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "multipleOf"); } if ((prior.Maximum == null && Maximum != null) || (prior.Maximum != null && !prior.Maximum.Equals(Maximum)) || prior.ExclusiveMaximum != ExclusiveMaximum) { // Flag stricter constraints for requests and relaxed constraints for responses. if (prior.ExclusiveMaximum != ExclusiveMaximum || context.Direction == DataDirection.None) context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "maximum"); else if (context.Direction == DataDirection.Request && Narrows(prior.Maximum, Maximum, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "maximum"); else if (context.Direction == DataDirection.Response && Widens(prior.Maximum, Maximum, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "maximum"); else if (Narrows(prior.Maximum, Maximum, false)) context.LogInfo(ComparisonMessages.ConstraintIsStronger, "maximum"); else if (Widens(prior.Maximum, Maximum, false)) context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "maximum"); } if ((prior.Minimum == null && Minimum != null) || (prior.Minimum != null && !prior.Minimum.Equals(Minimum)) || prior.ExclusiveMinimum != ExclusiveMinimum) { // Flag stricter constraints for requests and relaxed constraints for responses. if (prior.ExclusiveMinimum != ExclusiveMinimum || context.Direction == DataDirection.None) context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "minimum"); else if (context.Direction == DataDirection.Request && Narrows(prior.Minimum, Minimum, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "minimum"); else if (context.Direction == DataDirection.Response && Widens(prior.Minimum, Minimum, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "minimum"); else if (Narrows(prior.Minimum, Minimum, false)) context.LogInfo(ComparisonMessages.ConstraintIsStronger, "minimum"); else if (Widens(prior.Minimum, Minimum, false)) context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "minimum"); } if ((prior.MaxLength == null && MaxLength != null) || (prior.MaxLength != null && !prior.MaxLength.Equals(MaxLength))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "maxLength"); else if (context.Direction == DataDirection.Request && Narrows(prior.MaxLength, MaxLength, false)) context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "maxLength"); else if (context.Direction == DataDirection.Response && Widens(prior.MaxLength, MaxLength, false)) context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "maxLength"); else if (Narrows(prior.MaxLength, MaxLength, false)) context.LogInfo(ComparisonMessages.ConstraintIsStronger, "maxLength"); else if (Widens(prior.MaxLength, MaxLength, false)) context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "maxLength"); } if ((prior.MinLength == null && MinLength != null) || (prior.MinLength != null && !prior.MinLength.Equals(MinLength))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "minLength"); else if (context.Direction == DataDirection.Request && Narrows(prior.MinLength, MinLength, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "minimum"); else if (context.Direction == DataDirection.Response && Widens(prior.MinLength, MinLength, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "minimum"); else if (Narrows(prior.MinLength, MinLength, false)) context.LogInfo(ComparisonMessages.ConstraintIsStronger, "minLength"); else if (Widens(prior.MinLength, MinLength, false)) context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "minLength"); } if ((prior.Pattern == null && Pattern != null) || (prior.Pattern != null && !prior.Pattern.Equals(Pattern))) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "pattern"); } if ((prior.MaxItems == null && MaxItems != null) || (prior.MaxItems != null && !prior.MaxItems.Equals(MaxItems))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "maxItems"); else if (context.Direction == DataDirection.Request && Narrows(prior.MaxItems, MaxItems, false)) context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "maxItems"); else if (context.Direction == DataDirection.Response && Widens(prior.MaxItems, MaxItems, false)) context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "maxItems"); else if (Narrows(prior.MaxItems, MaxItems, false)) context.LogInfo(ComparisonMessages.ConstraintIsStronger, "maxItems"); else if (Widens(prior.MaxItems, MaxItems, false)) context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "maxItems"); } if ((prior.MinItems == null && MinItems != null) || (prior.MinItems != null && !prior.MinItems.Equals(MinItems))) { // Flag stricter constraints for requests and relaxed constraints for responses. if (context.Direction == DataDirection.None) context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "minItems"); else if (context.Direction == DataDirection.Request && Narrows(prior.MinItems, MinItems, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsStronger, "minItems"); else if (context.Direction == DataDirection.Response && Widens(prior.MinItems, MinItems, true)) context.LogBreakingChange(ComparisonMessages.ConstraintIsWeaker, "minItems"); else if (Narrows(prior.MinItems, MinItems, true)) context.LogInfo(ComparisonMessages.ConstraintIsStronger, "minItems"); else if (Widens(prior.MinItems, MinItems, true)) context.LogInfo(ComparisonMessages.ConstraintIsWeaker, "minItems"); } if (prior.UniqueItems != UniqueItems) { context.LogBreakingChange(ComparisonMessages.ConstraintChanged, "uniqueItems"); } }
public static void PopulateParameter(IVariable parameter, SwaggerObject swaggerObject) { if (swaggerObject == null) { throw new ArgumentNullException("swaggerObject"); } if (parameter == null) { throw new ArgumentNullException("parameter"); } parameter.IsRequired = swaggerObject.IsRequired; parameter.DefaultValue = swaggerObject.Default; if (IsSwaggerObjectConstant(swaggerObject)) { parameter.DefaultValue = swaggerObject.Enum[0]; parameter.IsConstant = true; } parameter.Documentation = swaggerObject.Description; parameter.CollectionFormat = swaggerObject.CollectionFormat; // tag the paramter with all the extensions from the swagger object parameter.Extensions.AddRange(swaggerObject.Extensions); SetConstraints(parameter.Constraints, swaggerObject); }