|
| 1 | +package code.api.v1_4_0 |
| 2 | + |
| 3 | +import code.api.util.CustomJsonFormats |
| 4 | +import code.util.Helper.MdcLoggable |
| 5 | +import net.liftweb.json._ |
| 6 | +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FeatureSpec, GivenWhenThen, Matchers} |
| 7 | + |
| 8 | +/** |
| 9 | + * Bug Condition Exploration Test for Nested Array Schema Generation |
| 10 | + * |
| 11 | + * CRITICAL: This test MUST FAIL on unfixed code - failure confirms the bug exists |
| 12 | + * DO NOT attempt to fix the test or the code when it fails |
| 13 | + * |
| 14 | + * This test encodes the expected behavior - it will validate the fix when it passes after implementation |
| 15 | + * GOAL: Surface counterexamples that demonstrate the bug exists |
| 16 | + * |
| 17 | + * Bug Condition: When translateEntity encounters a JArray containing another JArray, |
| 18 | + * it incorrectly generates nested objects with "arr" properties instead of proper nested array schema. |
| 19 | + * |
| 20 | + * Expected Behavior: Nested arrays should generate {"type": "array", "items": {"type": "array", ...}} |
| 21 | + * without object wrappers. |
| 22 | + */ |
| 23 | +class JSONFactory1_4_0NestedArrayTest extends FeatureSpec |
| 24 | + with BeforeAndAfterEach |
| 25 | + with GivenWhenThen |
| 26 | + with BeforeAndAfterAll |
| 27 | + with Matchers |
| 28 | + with MdcLoggable |
| 29 | + with CustomJsonFormats { |
| 30 | + |
| 31 | + feature("Bug Condition: Nested Array Schema Generation") { |
| 32 | + |
| 33 | + scenario("2-level nested array should generate correct nested array schema") { |
| 34 | + Given("A 2-level nested JArray: JArray(List(JArray(List(JInt(42)))))") |
| 35 | + val nestedArray = JArray(List(JArray(List(JInt(42))))) |
| 36 | + val testObject = JObject(List(JField("coordinates", nestedArray))) |
| 37 | + |
| 38 | + When("translateEntity is called on the nested array") |
| 39 | + val schema = JSONFactory1_4_0.translateEntity(testObject, false) |
| 40 | + |
| 41 | + Then("The schema should contain nested array types without object wrappers") |
| 42 | + logger.info(s"Generated schema for 2-level nested array: {$schema}") |
| 43 | + |
| 44 | + // Expected: {"type": "array", "items": {"type": "array", "items": {"type": "integer"}}} |
| 45 | + // Current (buggy): Contains "type": "object" with "properties": {"arr": ...} |
| 46 | + |
| 47 | + // Check that schema does NOT contain the buggy pattern with "arr" property |
| 48 | + schema should not include """"arr":""" |
| 49 | + |
| 50 | + // Check that schema contains proper nested array structure |
| 51 | + schema should include (""""type": "array"""") |
| 52 | + |
| 53 | + // Parse the schema to verify structure |
| 54 | + val parsedSchema = parse(schema) |
| 55 | + |
| 56 | + val coordinatesField = (parsedSchema \ "properties" \ "coordinates") |
| 57 | + (coordinatesField \ "type").extract[String] shouldBe "array" |
| 58 | + |
| 59 | + val itemsLevel1 = (coordinatesField \ "items") |
| 60 | + (itemsLevel1 \ "type").extract[String] shouldBe "array" |
| 61 | + |
| 62 | + // Should NOT contain "properties" with "arr" key |
| 63 | + (itemsLevel1 \ "properties" \ "arr") shouldBe JNothing |
| 64 | + |
| 65 | + val itemsLevel2 = (itemsLevel1 \ "items") |
| 66 | + (itemsLevel2 \ "type").extract[String] shouldBe "integer" |
| 67 | + } |
| 68 | + |
| 69 | + scenario("3-level nested array should generate correct nested array schema") { |
| 70 | + Given("A 3-level nested JArray: JArray(List(JArray(List(JArray(List(JString('value')))))))") |
| 71 | + val nestedArray = JArray(List(JArray(List(JArray(List(JString("value"))))))) |
| 72 | + val testObject = JObject(List(JField("data", nestedArray))) |
| 73 | + |
| 74 | + When("translateEntity is called on the nested array") |
| 75 | + val schema = JSONFactory1_4_0.translateEntity(testObject, false) |
| 76 | + |
| 77 | + Then("The schema should contain 3 levels of nested array types") |
| 78 | + logger.info(s"Generated schema for 3-level nested array: {$schema}") |
| 79 | + |
| 80 | + // Check that schema does NOT contain the buggy pattern with "arr" property |
| 81 | + schema should not include """"arr":""" |
| 82 | + |
| 83 | + val parsedSchema = parse(schema) |
| 84 | + |
| 85 | + val dataField = (parsedSchema \ "properties" \ "data") |
| 86 | + (dataField \ "type").extract[String] shouldBe "array" |
| 87 | + |
| 88 | + val itemsLevel1 = (dataField \ "items") |
| 89 | + (itemsLevel1 \ "type").extract[String] shouldBe "array" |
| 90 | + (itemsLevel1 \ "properties" \ "arr") shouldBe JNothing |
| 91 | + |
| 92 | + val itemsLevel2 = (itemsLevel1 \ "items") |
| 93 | + (itemsLevel2 \ "type").extract[String] shouldBe "array" |
| 94 | + (itemsLevel2 \ "properties" \ "arr") shouldBe JNothing |
| 95 | + |
| 96 | + val itemsLevel3 = (itemsLevel2 \ "items") |
| 97 | + (itemsLevel3 \ "type").extract[String] shouldBe "string" |
| 98 | + } |
| 99 | + |
| 100 | + scenario("4-level GeoJSON MultiPolygon coordinates should generate correct nested array schema") { |
| 101 | + Given("A 4-level nested JArray representing GeoJSON MultiPolygon coordinates") |
| 102 | + val coordinates = JArray(List( |
| 103 | + JArray(List( |
| 104 | + JArray(List( |
| 105 | + JArray(List(JDouble(102.0), JDouble(2.0))), |
| 106 | + JArray(List(JDouble(103.0), JDouble(2.0))), |
| 107 | + JArray(List(JDouble(103.0), JDouble(3.0))), |
| 108 | + JArray(List(JDouble(102.0), JDouble(3.0))), |
| 109 | + JArray(List(JDouble(102.0), JDouble(2.0))) |
| 110 | + )) |
| 111 | + )) |
| 112 | + )) |
| 113 | + val testObject = JObject(List(JField("coordinates", coordinates))) |
| 114 | + |
| 115 | + When("translateEntity is called on the GeoJSON coordinates") |
| 116 | + val schema = JSONFactory1_4_0.translateEntity(testObject, false) |
| 117 | + |
| 118 | + Then("The schema should contain 4 levels of nested array types terminating in number") |
| 119 | + logger.info(s"Generated schema for GeoJSON MultiPolygon: {$schema}") |
| 120 | + |
| 121 | + // Check that schema does NOT contain the buggy pattern with "arr" property |
| 122 | + schema should not include """"arr":""" |
| 123 | + |
| 124 | + val parsedSchema = parse(schema) |
| 125 | + |
| 126 | + val coordinatesField = (parsedSchema \ "properties" \ "coordinates") |
| 127 | + (coordinatesField \ "type").extract[String] shouldBe "array" |
| 128 | + |
| 129 | + val itemsLevel1 = (coordinatesField \ "items") |
| 130 | + (itemsLevel1 \ "type").extract[String] shouldBe "array" |
| 131 | + (itemsLevel1 \ "properties" \ "arr") shouldBe JNothing |
| 132 | + |
| 133 | + val itemsLevel2 = (itemsLevel1 \ "items") |
| 134 | + (itemsLevel2 \ "type").extract[String] shouldBe "array" |
| 135 | + (itemsLevel2 \ "properties" \ "arr") shouldBe JNothing |
| 136 | + |
| 137 | + val itemsLevel3 = (itemsLevel2 \ "items") |
| 138 | + (itemsLevel3 \ "type").extract[String] shouldBe "array" |
| 139 | + (itemsLevel3 \ "properties" \ "arr") shouldBe JNothing |
| 140 | + |
| 141 | + val itemsLevel4 = (itemsLevel3 \ "items") |
| 142 | + (itemsLevel4 \ "type").extract[String] shouldBe "number" |
| 143 | + } |
| 144 | + |
| 145 | + scenario("Empty nested array should be handled gracefully") { |
| 146 | + Given("An empty nested JArray: JArray(List(JArray(List())))") |
| 147 | + val emptyNestedArray = JArray(List(JArray(List()))) |
| 148 | + val testObject = JObject(List(JField("empty", emptyNestedArray))) |
| 149 | + |
| 150 | + When("translateEntity is called on the empty nested array") |
| 151 | + val schema = JSONFactory1_4_0.translateEntity(testObject, false) |
| 152 | + |
| 153 | + Then("The schema should handle the empty nested array gracefully") |
| 154 | + logger.debug(s"Generated schema for empty nested array: $schema") |
| 155 | + |
| 156 | + val parsedSchema = parse(schema) |
| 157 | + |
| 158 | + val emptyField = (parsedSchema \ "properties" \ "empty") |
| 159 | + (emptyField \ "type").extract[String] shouldBe "array" |
| 160 | + |
| 161 | + val itemsLevel1 = (emptyField \ "items") |
| 162 | + (itemsLevel1 \ "type").extract[String] shouldBe "array" |
| 163 | + |
| 164 | + // Should NOT contain "properties" with "arr" key |
| 165 | + (itemsLevel1 \ "properties" \ "arr") shouldBe JNothing |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments