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 @@ -969,7 +969,32 @@ public static boolean shouldGenerateMapModel(Schema schema) {
}

public static boolean shouldGenerateArrayModel(Schema schema) {
return ModelUtils.isGenerateAliasAsModel(schema) || !(schema.getProperties() == null || schema.getProperties().isEmpty());
if (ModelUtils.isGenerateAliasAsModel(schema)) {
return true;
}
// Generate if the array schema itself has properties (unusual but valid)
if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
return true;
}
// Generate if items are non-trivial (not a simple primitive alias like List<String>).
// After InlineModelResolver runs, complex inline items are extracted to components
// and replaced with a $ref, so we check for $ref as well as inline properties.
Schema<?> items = schema.getItems();
if (items != null) {
// Items resolved to a $ref (inline object was extracted to components)
if (items.get$ref() != null && !items.get$ref().isEmpty()) {
return true;
}
// Items with inline properties
if (items.getProperties() != null && !items.getProperties().isEmpty()) {
return true;
}
// Items with composed schema (oneOf/anyOf/allOf)
if (ModelUtils.isComposedSchema(items)) {
return true;
}
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public void testJdkHttpClientWithAndWithoutDiscriminator() {
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
List<File> files = generator.opts(configurator.toClientOptInput()).generate();

assertThat(files).hasSize(153);
assertThat(files).hasSize(156);
validateJavaSourceFiles(files);
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Dog.java")).content()
.contains("import xyz.abcdef.invoker.JSON;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,60 @@ public void getParentNameMultipleInterfacesTest() {
Schema composedSchema = allSchemas.get("RandomAnimalsResponse_animals_inner");
assertNull(ModelUtils.getParentName(composedSchema, allSchemas));
}

@Test
public void shouldGenerateArrayModelWithRefItems() {
// array with items.$ref should generate a model (most common case after InlineModelResolver)
Schema<?> items = new Schema<>();
items.set$ref("#/components/schemas/Foo");
Schema<?> arraySchema = new ArraySchema().items(items);
assertTrue(ModelUtils.shouldGenerateArrayModel(arraySchema));
}

@Test
public void shouldNotGenerateArrayModelWithPrimitiveItems() {
// array with simple string items is a plain alias (e.g. List<String>) — skip
Schema<?> arraySchema = new ArraySchema().items(new StringSchema());
assertFalse(ModelUtils.shouldGenerateArrayModel(arraySchema));
}

@Test
public void shouldNotGenerateArrayModelWithPrimitiveIntegerItems() {
// array with simple integer items is a plain alias (e.g. List<Integer>) — skip
Schema<?> arraySchema = new ArraySchema().items(new IntegerSchema());
assertFalse(ModelUtils.shouldGenerateArrayModel(arraySchema));
}

@Test
public void shouldGenerateArrayModelWithComposedItems() {
// array with oneOf items should generate a model
Schema<?> items = new Schema<>();
items.oneOf(List.of(new Schema<>().$ref("#/components/schemas/A"), new Schema<>().$ref("#/components/schemas/B")));
Schema<?> arraySchema = new ArraySchema().items(items);
assertTrue(ModelUtils.shouldGenerateArrayModel(arraySchema));
}

@Test
public void shouldGenerateArrayModelWithInlineObjectItems() {
// array with items that have inline properties should generate a model
Schema<?> items = new ObjectSchema();
items.addProperty("name", new StringSchema());
Schema<?> arraySchema = new ArraySchema().items(items);
assertTrue(ModelUtils.shouldGenerateArrayModel(arraySchema));
}

@Test
public void shouldNotGenerateArrayModelWithNullItems() {
// array with no items at all — alias, skip
Schema<?> arraySchema = new ArraySchema();
assertFalse(ModelUtils.shouldGenerateArrayModel(arraySchema));
}

@Test
public void shouldGenerateArrayModelWhenGenerateAliasAsModelExtensionIsSet() {
// with x-generate-alias-as-model extension, even a simple alias should generate
Schema<?> arraySchema = new ArraySchema().items(new StringSchema());
arraySchema.addExtension("x-generate-alias-as-model", true);
assertTrue(ModelUtils.shouldGenerateArrayModel(arraySchema));
}
}