Skip to content

bugfix/fix nested array schema generation for GeoJSON MultiPolygon coordinates#2765

Open
hongwei1 wants to merge 2 commits intoOpenBankProject:developfrom
hongwei1:obp-develop
Open

bugfix/fix nested array schema generation for GeoJSON MultiPolygon coordinates#2765
hongwei1 wants to merge 2 commits intoOpenBankProject:developfrom
hongwei1:obp-develop

Conversation

@hongwei1
Copy link
Copy Markdown
Contributor

Summary

Fix OpenAPI schema generator bug where deeply nested arrays (like GeoJSON MultiPolygon coordinates: number[][][][]) were incorrectly converted to nested objects with "arr" properties instead of proper nested array types.

Problem

When translateEntity encountered a JArray containing another JArray, it used reflection to extract fields from the JArray class, which exposed the internal arr field. This resulted in incorrect schema generation.

Before (Buggy):

{
  "type": "object",
  "properties": {
    "arr": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "arr": {...}
        }
      }
    }
  }
}

After (Fixed):

{
  "type": "array",
  "items": {
    "type": "array",
    "items": {
      "type": "integer"
    }
  }
}

Solution

Added explicit handling for JArray elements in the pattern matching logic:

  1. Helper Function (lines 658-666): isNestedArray to detect JArray containing JArray
  2. Early Return Logic (lines 684-705): Handles ALL JArray cases before reflection
  3. Explicit JArray Case (lines 695-697): Recursively processes nested arrays
  4. Safety Check (line 709): Prevents reflection extraction on JArray

Testing

All 17 tests passed (100% pass rate):

Bug Condition Tests (4/4 passed)

  • ✅ 2-level nested array
  • ✅ 3-level nested array
  • ✅ 4-level GeoJSON MultiPolygon
  • ✅ Empty nested array

Preservation Tests (13/13 passed)

  • ✅ Single-level arrays of primitives
  • ✅ Arrays of objects
  • ✅ Primitive types
  • ✅ Enum types
  • ✅ Wrapped values
  • ✅ Nested objects
  • ✅ Mixed field types

Zero regressions - all existing functionality preserved.

Files Modified

  1. obp-api/src/main/scala/code/api/v1_4_0/JSONFactory1_4_0.scala

    • Added isNestedArray helper function
    • Added early return for JArray handling
    • Added explicit JArray case in pattern matching
    • Added safety check for reflection
  2. obp-api/src/test/scala/code/api/v1_4_0/JSONFactory1_4_0NestedArrayTest.scala

    • Created bug condition exploration test with 4 scenarios
    • Fixed JSON path access

Impact

This fix enables correct OpenAPI schema generation for:

  • GeoJSON MultiPolygon coordinates (number[][][][])
  • Any deeply nested array structures
  • Complex data structures with multiple levels of array nesting

All existing functionality is preserved with zero regressions.

Commit

  • Commit: 998308005
  • Message: bugfix/fix nested array schema generation for GeoJSON MultiPolygon coordinates
  • Changes: 2 files, 51 insertions(+), 12 deletions(-)

@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant