|
1 | 1 | package org.openapitools.codegen; |
2 | 2 |
|
3 | 3 | import io.swagger.v3.oas.models.OpenAPI; |
| 4 | +import io.swagger.v3.oas.models.media.Schema; |
| 5 | +import io.swagger.v3.oas.models.media.IntegerSchema; |
4 | 6 | import org.openapitools.codegen.examples.ExampleGenerator; |
5 | 7 | import org.testng.annotations.Test; |
6 | 8 |
|
|
11 | 13 |
|
12 | 14 | import static org.testng.AssertJUnit.assertEquals; |
13 | 15 | import static org.testng.AssertJUnit.assertNull; |
| 16 | +import static org.testng.AssertJUnit.assertTrue; |
14 | 17 |
|
15 | 18 | public class ExampleGeneratorTest { |
16 | 19 | @Test |
@@ -334,4 +337,73 @@ public void generateFromResponseSchemaWithAnyOfComposedModel() { |
334 | 337 | assertEquals(String.format(Locale.ROOT, "{%n \"example_schema_property\" : \"example schema property value\"%n}"), examples.get(0).get("example")); |
335 | 338 | assertEquals("200", examples.get(0).get("statusCode")); |
336 | 339 | } |
| 340 | + |
| 341 | + @Test |
| 342 | + public void testExamplePropertyOrderPreservation() { |
| 343 | + OpenAPI openAPI = new OpenAPI(); |
| 344 | + |
| 345 | + // Create a schema with properties in a specific order |
| 346 | + Schema<?> testSchema = new Schema<>(); |
| 347 | + testSchema.setType("object"); |
| 348 | + |
| 349 | + // Use LinkedHashMap to preserve property order as defined in spec |
| 350 | + Map<String, Schema> properties = new LinkedHashMap<>(); |
| 351 | + |
| 352 | + // Add properties in the specific order: zebra, apple, mango, cherry, banana |
| 353 | + IntegerSchema zebraSchema = new IntegerSchema(); |
| 354 | + zebraSchema.setExample(1); |
| 355 | + properties.put("zebra", zebraSchema); |
| 356 | + |
| 357 | + IntegerSchema appleSchema = new IntegerSchema(); |
| 358 | + appleSchema.setExample(2); |
| 359 | + properties.put("apple", appleSchema); |
| 360 | + |
| 361 | + IntegerSchema mangoSchema = new IntegerSchema(); |
| 362 | + mangoSchema.setExample(3); |
| 363 | + properties.put("mango", mangoSchema); |
| 364 | + |
| 365 | + IntegerSchema cherrySchema = new IntegerSchema(); |
| 366 | + cherrySchema.setExample(4); |
| 367 | + properties.put("cherry", cherrySchema); |
| 368 | + |
| 369 | + IntegerSchema bananaSchema = new IntegerSchema(); |
| 370 | + bananaSchema.setExample(5); |
| 371 | + properties.put("banana", bananaSchema); |
| 372 | + |
| 373 | + testSchema.setProperties(properties); |
| 374 | + |
| 375 | + // Create examples map |
| 376 | + Map<String, Schema> examples = new HashMap<>(); |
| 377 | + examples.put("TestModel", testSchema); |
| 378 | + |
| 379 | + // Generate the example using the model name approach |
| 380 | + ExampleGenerator generator = new ExampleGenerator(examples, openAPI); |
| 381 | + Set<String> mediaTypeKeys = new TreeSet<>(); |
| 382 | + mediaTypeKeys.add("application/json"); |
| 383 | + |
| 384 | + List<Map<String, String>> generatedExamples = generator.generate(null, new ArrayList<>(mediaTypeKeys), "TestModel"); |
| 385 | + |
| 386 | + assertEquals(1, generatedExamples.size()); |
| 387 | + String exampleOutput = generatedExamples.get(0).get("example"); |
| 388 | + |
| 389 | + // Verify the example contains properties in the correct order |
| 390 | + // The order should be: zebra, apple, mango, cherry, banana |
| 391 | + assertTrue(exampleOutput.contains("\"zebra\" : 1")); |
| 392 | + assertTrue(exampleOutput.contains("\"apple\" : 2")); |
| 393 | + assertTrue(exampleOutput.contains("\"mango\" : 3")); |
| 394 | + assertTrue(exampleOutput.contains("\"cherry\" : 4")); |
| 395 | + assertTrue(exampleOutput.contains("\"banana\" : 5")); |
| 396 | + |
| 397 | + // Verify the order by checking the position of each field in the string |
| 398 | + int zebraPos = exampleOutput.indexOf("\"zebra\""); |
| 399 | + int applePos = exampleOutput.indexOf("\"apple\""); |
| 400 | + int mangoPos = exampleOutput.indexOf("\"mango\""); |
| 401 | + int cherryPos = exampleOutput.indexOf("\"cherry\""); |
| 402 | + int bananaPos = exampleOutput.indexOf("\"banana\""); |
| 403 | + |
| 404 | + assertTrue("zebra should come before apple", zebraPos < applePos); |
| 405 | + assertTrue("apple should come before mango", applePos < mangoPos); |
| 406 | + assertTrue("mango should come before cherry", mangoPos < cherryPos); |
| 407 | + assertTrue("cherry should come before banana", cherryPos < bananaPos); |
| 408 | + } |
337 | 409 | } |
0 commit comments