Skip to content

Commit 1597e9a

Browse files
committed
changed equal validation for changed example to properly validate arrays of primitive types
changed equal validation for changed example to properly validate arrays of primitive types
1 parent a674aa1 commit 1597e9a

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

core/src/main/java/org/openapitools/openapidiff/core/model/ChangedExample.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.openapitools.openapidiff.core.model;
22

3+
import java.lang.reflect.Array;
34
import java.util.Objects;
45

56
public class ChangedExample implements Changed {
@@ -30,9 +31,35 @@ public void setRightExample(Object rightExample) {
3031

3132
@Override
3233
public DiffResult isChanged() {
33-
if (!Objects.equals(leftExample, rightExample)) {
34+
DiffResult arrayDiff = compareArrays();
35+
if (arrayDiff != null) {
36+
return arrayDiff;
37+
}
38+
39+
return Objects.equals(leftExample, rightExample) ? DiffResult.NO_CHANGES : DiffResult.METADATA;
40+
}
41+
42+
private DiffResult compareArrays() {
43+
if (leftExample == null || rightExample == null) {
44+
return null;
45+
}
46+
47+
if (!leftExample.getClass().isArray() || !rightExample.getClass().isArray()) {
48+
return null;
49+
}
50+
51+
int leftLength = Array.getLength(leftExample);
52+
53+
if (leftLength != Array.getLength(rightExample)) {
3454
return DiffResult.METADATA;
3555
}
56+
57+
for (int i = 0; i < leftLength; i++) {
58+
if (!Objects.deepEquals(Array.get(leftExample, i), Array.get(rightExample, i))) {
59+
return DiffResult.METADATA;
60+
}
61+
}
62+
3663
return DiffResult.NO_CHANGES;
3764
}
3865

core/src/test/java/org/openapitools/openapidiff/core/output/ConsoleRenderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.ByteArrayOutputStream;
66
import java.io.OutputStreamWriter;
7+
import org.assertj.core.api.Assertions;
78
import org.junit.jupiter.api.Test;
89
import org.openapitools.openapidiff.core.OpenApiCompare;
910
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
@@ -76,4 +77,19 @@ public void renderShowsWhatsChangedSectionWithCorrectFormattingWhenEndpointIsCha
7677
.contains("What's Changed")
7778
.containsSubsequence("- GET /widgets", "Parameter:", "- Changed query-param-1 in query");
7879
}
80+
81+
@Test
82+
void renderShowsNoDifferencesWhenCSVMediaTypeResponseExampleIsByteArray() {
83+
ConsoleRender render = new ConsoleRender();
84+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
85+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
86+
ChangedOpenApi diff =
87+
OpenApiCompare.fromLocations(
88+
"issue-828-binary-example-for-csv-media-type.yaml",
89+
"issue-828-binary-example-for-csv-media-type.yaml");
90+
render.render(diff, outputStreamWriter);
91+
Assertions.assertThat(outputStream.toString()).isNotBlank();
92+
Assertions.assertThat(outputStream.toString())
93+
.hasToString("No differences. Specifications are equivalents");
94+
}
7995
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.1
2+
info:
3+
title: My API
4+
version: 1.0.0
5+
paths:
6+
/api/v1/csv:
7+
get:
8+
operationId: someCsvEndpoint
9+
responses:
10+
"200":
11+
content:
12+
text/csv:
13+
schema:
14+
type: string
15+
format: binary
16+
example: Example
17+
description: Successful operation
18+
security:
19+
- Bearer: []
20+
summary: Download a CSV or something
21+
components:
22+
securitySchemes:
23+
Bearer:
24+
bearerFormat: JWT
25+
scheme: bearer
26+
type: http

0 commit comments

Comments
 (0)