Skip to content

Commit 856f2c7

Browse files
committed
Update UnitTestSwaggerUtils to deterministic
Update TestSwagger without adding dependencies Update UnitTestSwaggerUtils.java Remove unused import
1 parent 842c55e commit 856f2c7

File tree

1 file changed

+16
-3
lines changed
  • swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/unittest

1 file changed

+16
-3
lines changed

swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/unittest/UnitTestSwaggerUtils.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
import java.io.IOException;
2121
import java.net.URL;
2222
import java.nio.charset.StandardCharsets;
23-
import java.util.Objects;
2423

2524
import org.apache.commons.io.IOUtils;
2625
import org.apache.servicecomb.swagger.generator.SwaggerGenerator;
2726
import org.junit.jupiter.api.Assertions;
2827

2928
import com.fasterxml.jackson.core.JsonProcessingException;
29+
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.fasterxml.jackson.databind.ObjectMapper;
3031
import com.fasterxml.jackson.databind.ObjectWriter;
32+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
3133

3234
import io.swagger.v3.core.util.Yaml;
3335
import io.swagger.v3.oas.models.OpenAPI;
@@ -81,8 +83,19 @@ public static SwaggerGenerator testSwagger(String resPath, Class<?> cls, String.
8183
expectSchema = expectSchema.substring(offset + 4);
8284
}
8385

84-
if (!Objects.equals(expectSchema, schema)) {
85-
Assertions.assertEquals(expectSchema, schema);
86+
try {
87+
ObjectMapper yaml = new ObjectMapper(new YAMLFactory());
88+
JsonNode expected = yaml.readTree(expectSchema);
89+
JsonNode actual = yaml.readTree(schema);
90+
91+
if (!actual.equals(expected)) {
92+
ObjectMapper json = new ObjectMapper();
93+
String expectedPretty = json.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
94+
String actualPretty = json.writerWithDefaultPrettyPrinter().writeValueAsString(actual);
95+
Assertions.fail("OpenAPI mismatch.\n=== EXPECTED ===\n" + expectedPretty + "\n=== ACTUAL ===\n" + actualPretty);
96+
}
97+
} catch (Exception e) {
98+
Assertions.fail("Failed to parse/compare OpenAPI YAML: " + e.getMessage(), e);
8699
}
87100

88101
return generator;

0 commit comments

Comments
 (0)