From bda65915691c638d311b0db6bcadd4b0a27d2412 Mon Sep 17 00:00:00 2001 From: Ewa Ostrowska Date: Thu, 6 Nov 2025 11:57:38 +0100 Subject: [PATCH] Add minimal bug reproduction tests for issue #4341 - Test 1: Reproduces ArraySchema.arraySchema.requiredMode not working - Test 2: Validates workaround using schema.requiredMode - Minimal test models - Focused on issue reproduction only See: https://github.com/swagger-api/swagger-core/issues/4341 --- .../swagger/v3/core/issues/Issue4341Test.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4341Test.java diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4341Test.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4341Test.java new file mode 100644 index 0000000000..4a55d720ff --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4341Test.java @@ -0,0 +1,96 @@ +package io.swagger.v3.core.issues; + +import io.swagger.v3.core.converter.ModelConverters; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.models.media.ObjectSchema; +import org.testng.annotations.Test; + +import java.util.List; +import java.util.Map; + +import static org.testng.Assert.*; + +/** + * Reproduces GitHub Issue #4341 + * ArraySchema.arraySchema.requiredMode should control array requirement + * + * Problem: After replacing 'required' with 'requiredMode', ArraySchema.arraySchema.requiredMode doesn't work + * Expected: Field marked as required in arraySchema should appear in required array + * Actual: Required mode on arraySchema is ignored; must use schema.requiredMode as workaround + * + * @see https://github.com/swagger-api/swagger-core/issues/4341 + */ +public class Issue4341Test { + + // Test model - using ArraySchema with requiredMode on arraySchema + static class PersonWithArraySchemaRequired { + @ArraySchema( + arraySchema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED) + ) + private List addresses; + + public List getAddresses() { + return addresses; + } + + public void setAddresses(List addresses) { + this.addresses = addresses; + } + } + + // Workaround model - using requiredMode on schema instead + static class PersonWithSchemaRequired { + @ArraySchema( + arraySchema = @Schema(description = "The person's addresses"), + schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED) + ) + private List addresses; + + public List getAddresses() { + return addresses; + } + + public void setAddresses(List addresses) { + this.addresses = addresses; + } + } + + @Test(description = "Reproduces issue #4341: ArraySchema.arraySchema.requiredMode should mark field as required") + public void testArraySchemaRequiredMode() { + Map schemas = ModelConverters.getInstance() + .readAll(PersonWithArraySchemaRequired.class); + + assertNotNull(schemas); + io.swagger.v3.oas.models.media.Schema personSchema = schemas.get("PersonWithArraySchemaRequired"); + assertNotNull(personSchema, "Schema should be generated"); + assertTrue(personSchema instanceof ObjectSchema); + + ObjectSchema objectSchema = (ObjectSchema) personSchema; + List required = objectSchema.getRequired(); + + // This is the bug - required should contain "addresses" but it doesn't + assertNotNull(required, "Required array should not be null when arraySchema has requiredMode=REQUIRED"); + assertTrue(required != null && required.contains("addresses"), + "Field 'addresses' should be in required array when arraySchema has requiredMode=REQUIRED"); + } + + @Test(description = "Workaround: Using schema.requiredMode works correctly") + public void testWorkaround() { + Map schemas = ModelConverters.getInstance() + .readAll(PersonWithSchemaRequired.class); + + assertNotNull(schemas); + io.swagger.v3.oas.models.media.Schema personSchema = schemas.get("PersonWithSchemaRequired"); + assertNotNull(personSchema, "Schema should be generated"); + assertTrue(personSchema instanceof ObjectSchema); + + ObjectSchema objectSchema = (ObjectSchema) personSchema; + List required = objectSchema.getRequired(); + + // Workaround works - schema.requiredMode is respected + assertNotNull(required, "Required array should exist"); + assertTrue(required.contains("addresses"), + "Workaround: Field 'addresses' should be in required array when schema has requiredMode=REQUIRED"); + } +}