public virtual ChangedSchemaBO Diff <T>(
            OpenApiComponents leftComponents,
            OpenApiComponents rightComponents,
            T left,
            T right,
            DiffContextBO context)
            where T : OpenApiSchema
        {
            var leftEnumStrings  = left.Enum.Select(x => ((IOpenApiPrimitive)x)?.GetValueString()).ToList();
            var rightEnumStrings = right.Enum.Select(x => ((IOpenApiPrimitive)x)?.GetValueString()).ToList();
            var leftDefault      = (IOpenApiPrimitive)left.Default;
            var rightDefault     = (IOpenApiPrimitive)right.Default;

            var changedEnum =
                ListDiff.Diff(new ChangedEnumBO(leftEnumStrings, rightEnumStrings, context));

            ChangedSchema.Context            = context;
            ChangedSchema.OldSchema          = left;
            ChangedSchema.NewSchema          = right;
            ChangedSchema.IsChangeDeprecated = !left.Deprecated && right.Deprecated;
            ChangedSchema.IsChangeTitle      = left.Title != right.Title;
            ChangedSchema.Required           =
                ListDiff.Diff(new ChangedRequiredBO(left.Required.ToList(), right.Required.ToList(), context));
            ChangedSchema.IsChangeDefault = leftDefault?.GetValueString() != rightDefault?.GetValueString();
            ChangedSchema.Enumeration     = changedEnum;
            ChangedSchema.IsChangeFormat  = left.Format != right.Format;
            ChangedSchema.ReadOnly        = new ChangedReadOnlyBO(left.ReadOnly, right.ReadOnly, context);
            ChangedSchema.WriteOnly       = new ChangedWriteOnlyBO(left.WriteOnly, right.WriteOnly, context);
            ChangedSchema.MinLength       = new ChangedMinLengthBO(left.MinLength, right.MinLength, context);
            ChangedSchema.MaxLength       = new ChangedMaxLengthBO(left.MaxLength, right.MaxLength, context);

            var extendedDiff = OpenApiDiff.ExtensionsDiff.Diff(left.Extensions, right.Extensions, context);

            if (extendedDiff != null)
            {
                ChangedSchema.Extensions = extendedDiff;
            }
            var metaDataDiff = OpenApiDiff.MetadataDiff.Diff(left.Description, right.Description, context);

            if (metaDataDiff != null)
            {
                ChangedSchema.Description = metaDataDiff;
            }

            var leftProperties  = left.Properties;
            var rightProperties = right.Properties;
            var propertyDiff    = MapKeyDiff <string, OpenApiSchema> .Diff(leftProperties, rightProperties);

            foreach (var s in propertyDiff.SharedKey)
            {
                var diff = OpenApiDiff
                           .SchemaDiff
                           .Diff(leftProperties[s], rightProperties[s], Required(context, s, right.Required));

                if (diff != null)
                {
                    ChangedSchema.ChangedProperties.Add(s, diff);
                }
            }

            CompareAdditionalProperties(left, right, context);

            var allIncreasedProperties = FilterProperties(TypeEnum.Added, propertyDiff.Increased, context);

            foreach (var(key, value) in allIncreasedProperties)
            {
                ChangedSchema.IncreasedProperties.Add(key, value);
            }
            var allMissingProperties = FilterProperties(TypeEnum.Removed, propertyDiff.Missing, context);

            foreach (var(key, value) in allMissingProperties)
            {
                ChangedSchema.MissingProperties.Add(key, value);
            }

            return(IsApplicable(context));
        }
Esempio n. 2
0
        public override ChangedSchemaBO Diff <T>(OpenApiComponents leftComponents,
                                                 OpenApiComponents rightComponents, T left,
                                                 T right, DiffContextBO context)
        {
            if (left.GetSchemaType() == SchemaTypeEnum.ComposedSchema)
            {
                if (!left.OneOf.IsNullOrEmpty() || !right.OneOf.IsNullOrEmpty())
                {
                    var leftDis  = left.Discriminator;
                    var rightDis = right.Discriminator;
                    if (leftDis == null ||
                        rightDis == null ||
                        leftDis.PropertyName == null ||
                        rightDis.PropertyName == null)
                    {
                        throw new ArgumentException(
                                  "discriminator or property not found for oneOf schema");
                    }

                    if (leftDis.PropertyName != rightDis.PropertyName ||
                        left.OneOf.IsNullOrEmpty() ||
                        right.OneOf.IsNullOrEmpty())
                    {
                        ChangedSchema.OldSchema = left;
                        ChangedSchema.NewSchema = right;
                        ChangedSchema.DiscriminatorPropertyChanged = true;
                        ChangedSchema.Context = context;
                        return(ChangedSchema);
                    }

                    var leftMapping  = GetMapping(left);
                    var rightMapping = GetMapping(right);

                    var mappingDiff = MapKeyDiff <string, OpenApiSchema> .Diff(GetSchema(leftComponents, leftMapping),
                                                                               GetSchema(rightComponents, rightMapping));

                    var changedMapping = new Dictionary <string, ChangedSchemaBO>();
                    foreach (var refId in mappingDiff.SharedKey)
                    {
                        var leftReference = leftComponents.Schemas.Values
                                            .First(x => x.Reference.ReferenceV3 == leftMapping[refId]).Reference;
                        var rightReference = rightComponents.Schemas.Values
                                             .First(x => x.Reference.ReferenceV3 == rightMapping[refId]).Reference;

                        var leftSchema = new OpenApiSchema {
                            Reference = leftReference
                        };
                        var rightSchema = new OpenApiSchema {
                            Reference = rightReference
                        };
                        var changedSchema = OpenApiDiff.SchemaDiff
                                            .Diff(leftSchema, rightSchema, context.CopyWithRequired(true));
                        if (changedSchema != null)
                        {
                            changedMapping.Add(refId, changedSchema);
                        }
                    }

                    ChangedSchema.OneOfSchema = new ChangedOneOfSchemaBO(leftMapping, rightMapping, context)
                    {
                        Increased = mappingDiff.Increased,
                        Missing   = mappingDiff.Missing,
                        Changed   = changedMapping
                    };
                }

                return(base.Diff(leftComponents, rightComponents, left, right, context));
            }

            return(OpenApiDiff.SchemaDiff.GetTypeChangedSchema(left, right, context));
        }