Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
}

processedModels.add(name);
Map<String, Object> values = new HashMap<>();
Map<String, Object> values = new LinkedHashMap<>();
LOGGER.debug("Resolving model '{}' to example", name);
if (schema.getExample() != null) {
LOGGER.debug("Using example from spec: {}", schema.getExample());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.openapitools.codegen;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import org.openapitools.codegen.examples.ExampleGenerator;
import org.testng.annotations.Test;

Expand All @@ -11,6 +13,7 @@

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNull;
import static org.testng.AssertJUnit.assertTrue;

public class ExampleGeneratorTest {
@Test
Expand Down Expand Up @@ -334,4 +337,77 @@ public void generateFromResponseSchemaWithAnyOfComposedModel() {
assertEquals(String.format(Locale.ROOT, "{%n \"example_schema_property\" : \"example schema property value\"%n}"), examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}

@Test
public void testExamplePropertyOrderPreservation() {
OpenAPI openAPI = new OpenAPI();

// Create a schema with properties in a specific order
Schema<?> testSchema = new Schema<>();
testSchema.setType("object");

// Use LinkedHashMap to preserve property order as defined in spec
Map<String, Schema> properties = new LinkedHashMap<>();

// Add properties in the specific order: zebra, apple, mango, cherry, banana
IntegerSchema zebraSchema = new IntegerSchema();
zebraSchema.setExample(1);
properties.put("zebra", zebraSchema);

IntegerSchema appleSchema = new IntegerSchema();
appleSchema.setExample(2);
properties.put("apple", appleSchema);

IntegerSchema mangoSchema = new IntegerSchema();
mangoSchema.setExample(3);
properties.put("mango", mangoSchema);

IntegerSchema cherrySchema = new IntegerSchema();
cherrySchema.setExample(4);
properties.put("cherry", cherrySchema);

IntegerSchema bananaSchema = new IntegerSchema();
bananaSchema.setExample(5);
properties.put("banana", bananaSchema);

testSchema.setProperties(properties);

// Create examples map
Map<String, Schema> examples = new HashMap<>();
examples.put("TestModel", testSchema);

// Generate the example using the model name approach
ExampleGenerator generator = new ExampleGenerator(examples, openAPI);
Set<String> mediaTypeKeys = new TreeSet<>();
mediaTypeKeys.add("application/json");

List<Map<String, String>> generatedExamples = generator.generate(null, new ArrayList<>(mediaTypeKeys), "TestModel");

assertEquals(1, generatedExamples.size());
String exampleOutput = generatedExamples.get(0).get("example");

System.out.println("Generated example output: " + exampleOutput);

// Verify the example contains properties in the correct order
// The order should be: zebra, apple, mango, cherry, banana
assertTrue(exampleOutput.contains("\"zebra\" : 1"));
assertTrue(exampleOutput.contains("\"apple\" : 2"));
assertTrue(exampleOutput.contains("\"mango\" : 3"));
assertTrue(exampleOutput.contains("\"cherry\" : 4"));
assertTrue(exampleOutput.contains("\"banana\" : 5"));

// Verify the order by checking the position of each field in the string
int zebraPos = exampleOutput.indexOf("\"zebra\"");
int applePos = exampleOutput.indexOf("\"apple\"");
int mangoPos = exampleOutput.indexOf("\"mango\"");
int cherryPos = exampleOutput.indexOf("\"cherry\"");
int bananaPos = exampleOutput.indexOf("\"banana\"");

System.out.println("Field positions: zebra=" + zebraPos + ", apple=" + applePos + ", mango=" + mangoPos + ", cherry=" + cherryPos + ", banana=" + bananaPos);

assertTrue("zebra should come before apple", zebraPos < applePos);
assertTrue("apple should come before mango", applePos < mangoPos);
assertTrue("mango should come before cherry", mangoPos < cherryPos);
assertTrue("cherry should come before banana", cherryPos < bananaPos);
}
}