-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathChangedSchema.java
84 lines (76 loc) · 3.93 KB
/
ChangedSchema.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.qdesrame.openapi.diff.model;
import com.qdesrame.openapi.diff.model.schema.ChangedExtensions;
import com.qdesrame.openapi.diff.model.schema.ChangedReadOnly;
import com.qdesrame.openapi.diff.model.schema.ChangedWriteOnly;
import com.qdesrame.openapi.diff.utils.ChangedUtils;
import io.swagger.v3.oas.models.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import java.util.HashMap;
import java.util.Map;
/**
* Created by adarsh.sharma on 22/12/17.
*/
@Getter
@Setter
public class ChangedSchema implements Changed {
protected DiffContext context;
protected Schema oldSchema;
protected Schema newSchema;
protected String type;
protected Map<String, ChangedSchema> changedProperties;
protected Map<String, Schema> increasedProperties;
protected Map<String, Schema> missingProperties;
protected boolean changeDeprecated;
protected boolean changeDescription;
protected boolean changeTitle;
protected ListDiff<String> changeRequired;
protected boolean changeDefault;
protected ListDiff changeEnum;
protected boolean changeFormat;
protected ChangedReadOnly changedReadOnly;
protected ChangedWriteOnly changedWriteOnly;
protected boolean changedType;
protected boolean changedMaxLength;
protected boolean discriminatorPropertyChanged;
protected ChangedOneOfSchema changedOneOfSchema;
protected ChangedSchema addPropChangedSchema;
protected ChangedExtensions changedExtensions;
public ChangedSchema() {
increasedProperties = new HashMap<>();
missingProperties = new HashMap<>();
changedProperties = new HashMap<>();
}
@Override
public DiffResult isChanged() {
if (!changedType && (oldSchema == null && newSchema == null || oldSchema != null && newSchema != null)
&& ChangedUtils.isUnchanged(changedWriteOnly) && ChangedUtils.isUnchanged(changedReadOnly)
&& !changedMaxLength && (changeEnum == null || changeEnum.isUnchanged())
&& !changeFormat && increasedProperties.size() == 0 && missingProperties.size() == 0
&& changedProperties.values().size() == 0 && !changeDeprecated
&& (changeRequired == null || changeRequired.isUnchanged()) && !discriminatorPropertyChanged
&& ChangedUtils.isUnchanged(addPropChangedSchema) && ChangedUtils.isUnchanged(changedOneOfSchema)
&& ChangedUtils.isUnchanged(changedExtensions)) {
return DiffResult.NO_CHANGES;
}
boolean backwardCompatibleForRequest = (changeEnum == null || changeEnum.getMissing().isEmpty()) &&
(changeRequired == null || CollectionUtils.isEmpty(changeRequired.getIncreased())) &&
(oldSchema != null || newSchema == null) &&
(!changedMaxLength || newSchema.getMaxLength() == null ||
(oldSchema.getMaxLength() != null && oldSchema.getMaxLength() <= newSchema.getMaxLength()));
boolean backwardCompatibleForResponse = (changeEnum == null || changeEnum.getIncreased().isEmpty()) &&
missingProperties.isEmpty() &&
(oldSchema == null || newSchema != null) &&
(!changedMaxLength || oldSchema.getMaxLength() == null ||
(newSchema.getMaxLength() != null && newSchema.getMaxLength() <= oldSchema.getMaxLength()));
if ((context.isRequest() && backwardCompatibleForRequest || context.isResponse() && backwardCompatibleForResponse)
&& !changedType && !discriminatorPropertyChanged && ChangedUtils.isCompatible(changedOneOfSchema)
&& ChangedUtils.isCompatible(addPropChangedSchema)
&& changedProperties.values().stream().allMatch(Changed::isCompatible)
&& ChangedUtils.isCompatible(changedExtensions)) {
return DiffResult.COMPATIBLE;
}
return DiffResult.INCOMPATIBLE;
}
}