Skip to content

Commit 9773e1a

Browse files
committed
Fix DiffContext bug
1 parent bc44964 commit 9773e1a

File tree

8 files changed

+88
-13
lines changed

8 files changed

+88
-13
lines changed

src/main/java/com/qdesrame/openapi/diff/compare/ApiResponseDiff.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public Optional<ChangedApiResponse> diff(ApiResponses left, ApiResponses right,
2929
changedApiResponse.setAddResponses(responseMapKeyDiff.getIncreased());
3030
changedApiResponse.setMissingResponses(responseMapKeyDiff.getMissing());
3131
List<String> sharedResponseCodes = responseMapKeyDiff.getSharedKey();
32-
context.setRequest(false);
33-
context.setResponse(true);
3432
Map<String, ChangedResponse> resps = new HashMap<>();
3533
for (String responseCode : sharedResponseCodes) {
3634
openApiDiff.getResponseDiff().diff(left.get(responseCode), right.get(responseCode), context)

src/main/java/com/qdesrame/openapi/diff/compare/OperationDiff.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Optional<ChangedOperation> diff(Operation oldOperation, Operation newOper
2929
changedOperation.setDeprecated(!Boolean.TRUE.equals(oldOperation.getDeprecated()) && Boolean.TRUE.equals(newOperation.getDeprecated()));
3030

3131
if (oldOperation.getRequestBody() != null || newOperation.getRequestBody() != null) {
32-
openApiDiff.getRequestBodyDiff().diff(oldOperation.getRequestBody(), newOperation.getRequestBody(), context)
32+
openApiDiff.getRequestBodyDiff().diff(oldOperation.getRequestBody(), newOperation.getRequestBody(), context.copyAsRequest())
3333
.ifPresent(changedOperation::setChangedRequestBody);
3434
}
3535

@@ -40,7 +40,7 @@ public Optional<ChangedOperation> diff(Operation oldOperation, Operation newOper
4040
});
4141

4242
if (oldOperation.getResponses() != null || newOperation.getResponses() != null) {
43-
openApiDiff.getApiResponseDiff().diff(oldOperation.getResponses(), newOperation.getResponses(), context)
43+
openApiDiff.getApiResponseDiff().diff(oldOperation.getResponses(), newOperation.getResponses(), context.copyAsResponse())
4444
.ifPresent(changedOperation::setChangedApiResponse);
4545
}
4646

src/main/java/com/qdesrame/openapi/diff/compare/PathDiff.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public Optional<ChangedPath> diff(PathItem left, PathItem right, DiffContext con
3131
for (PathItem.HttpMethod method : sharedMethods) {
3232
Operation oldOperation = oldOperationMap.get(method);
3333
Operation newOperation = newOperationMap.get(method);
34-
context.setMethod(method);
35-
openApiDiff.getOperationDiff().diff(oldOperation, newOperation, context).ifPresent(changedPath.getChanged()::add);
34+
openApiDiff.getOperationDiff().diff(oldOperation, newOperation, context.copyWithMethod(method)).ifPresent(changedPath.getChanged()::add);
3635
}
3736
return isChanged(changedPath);
3837
}

src/main/java/com/qdesrame/openapi/diff/compare/RequestBodyDiff.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ protected Optional<ChangedRequestBody> computeDiff(HashSet<String> refSet, Reque
4646
newRequestContent = newRequestBody.getContent();
4747
}
4848
}
49-
context.setRequest(true);
50-
context.setResponse(false);
51-
5249
ChangedRequestBody changedRequestBody = new ChangedRequestBody(oldRequestBody, newRequestBody, context);
5350

5451
boolean leftRequired = oldRequestBody != null && Boolean.TRUE.equals(oldRequestBody.getRequired());
Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,93 @@
11
package com.qdesrame.openapi.diff.model;
22

33
import io.swagger.v3.oas.models.PathItem;
4-
import lombok.Getter;
5-
import lombok.Setter;
64

5+
import java.util.HashMap;
76
import java.util.Map;
87

98
/**
109
* Created by Quentin Desramé on 04/04/17.
1110
*/
12-
@Getter
13-
@Setter
1411
public class DiffContext {
1512

1613
private String url;
1714
private Map<String, String> parameters;
1815
private PathItem.HttpMethod method;
1916
private boolean response;
2017
private boolean request;
18+
19+
public DiffContext() {
20+
parameters = new HashMap<>();
21+
response = false;
22+
request = true;
23+
}
24+
25+
public DiffContext copyWithMethod(PathItem.HttpMethod method) {
26+
return copy().setMethod(method);
27+
}
28+
29+
public DiffContext copyAsRequest() {
30+
return copy().setRequest();
31+
}
32+
33+
public DiffContext copyAsResponse() {
34+
return copy().setResponse();
35+
}
36+
37+
private DiffContext setRequest() {
38+
this.request = true;
39+
this.response = false;
40+
return this;
41+
}
42+
43+
private DiffContext setResponse() {
44+
this.response = true;
45+
this.request = false;
46+
return this;
47+
}
48+
49+
public boolean isResponse() {
50+
return this.response;
51+
}
52+
53+
public boolean isRequest() {
54+
return this.request;
55+
}
56+
57+
public String getUrl() {
58+
return url;
59+
}
60+
61+
public DiffContext setUrl(String url) {
62+
this.url = url;
63+
return this;
64+
}
65+
66+
public PathItem.HttpMethod getMethod() {
67+
return method;
68+
}
69+
70+
private DiffContext setMethod(PathItem.HttpMethod method) {
71+
this.method = method;
72+
return this;
73+
}
74+
75+
private DiffContext copy() {
76+
DiffContext context = new DiffContext();
77+
context.url = this.url;
78+
context.parameters = this.parameters;
79+
context.method = this.method;
80+
context.response = this.response;
81+
context.request = this.request;
82+
return context;
83+
}
84+
85+
public Map<String, String> getParameters() {
86+
return parameters;
87+
}
88+
89+
public DiffContext setParameters(Map<String, String> parameters) {
90+
this.parameters = parameters;
91+
return this;
92+
}
2193
}

src/test/resources/backwardCompatibility/bc_2.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ components:
131131
properties:
132132
bark:
133133
type: string
134+
test:
135+
writeOnly: true
136+
type: string
134137
Lizard:
135138
type: object
136139
properties:

src/test/resources/backwardCompatibility/bc_3.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ components:
144144
properties:
145145
bark:
146146
type: string
147+
test:
148+
writeOnly: true
149+
type: string
147150
Lizard:
148151
type: object
149152
properties:

src/test/resources/backwardCompatibility/bc_4.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ components:
136136
properties:
137137
bark:
138138
type: string
139+
test:
140+
writeOnly: true
141+
type: string
139142
Lizard:
140143
type: object
141144
properties:

0 commit comments

Comments
 (0)