Skip to content

A required field cannot be nested beneath an absent optional field #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Sep 28, 2017 · 4 comments
Closed

A required field cannot be nested beneath an absent optional field #429

ghost opened this issue Sep 28, 2017 · 4 comments
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Sep 28, 2017

I'm having some problems after #402, but don't know if this suppose to be in this ways or not.

I have a json with structure like that:

"aa": [
  {
    "bb": "some",
    "cc": {
      "dd": "some"
    }
  },
  {
    "bb": "something else",
    "cc": {
      "dd": "something more"
    }
  }
]

So, "cc" node is optional, but "dd" node is required.

After #402 I have to declare both "cc" and "dd" as optional, otherwise test fails. Which feels kind of wrong - if "cc" present, then it MUST have "dd".

@wilkinsona
Copy link
Member

wilkinsona commented Sep 28, 2017

Interesting use case. I hadn't considered people nesting a required field beneath an optional one, and wanting the required field to only actually be required when the optional field is present.

The behaviour you're seeing now has always been the case without an array, for example with a payload like this:

"aa": {
  "bb": "some",
  "cc": {
    "dd": "some"
  }
}

You'd have the same problem if aa.cc was optional and aa.cc.dd was required. #402 has just made this apply to arrays too by removing an inconsistency.

I'll see what can be done to support your requirement. It may be possible to fix this in 1.2.x if it can be done without an API change.

@wilkinsona wilkinsona changed the title Weird behaviour after #402 An required field cannot be nested beneath an absent optional field Sep 28, 2017
@joemccall86
Copy link
Contributor

A more concrete example is shapes on a map. For example, our REST endpoint will index shapes. Each shape has an optional center point, optional points for polygons, and optional pointNorthEast and pointSouthWest for rectangles. If these points exist, they must have a latitude and longitude.

Sample response:

[ {
  "id" : 1,
  "alpha" : 1.0,
  "colorHex" : "#FF0000",
  "colorLightenedHex" : "#FFCCCC",
  "type" : "CIRCLE",
  "center" : {
    "id" : 1,
    "latitude" : 21.234589,
    "longitude" : -81.345897
  },
  "radius" : 35.0
}, {
  "id" : 2,
  "alpha" : 0.75,
  "colorHex" : "#FF0000",
  "colorLightenedHex" : "#FFCCCC",
  "type" : "POLYGON",
  "points" : [ {
    "id" : 2,
    "latitude" : 21.234589,
    "longitude" : -81.345897
  }, {
    "id" : 3,
    "latitude" : 21.222222,
    "longitude" : -81.888888
  } ]
}]
// ...
responseFields(
                        fieldWithPath('[].points').optional().description('the list of points on the polygon or polyline, if applicable'),
                        fieldWithPath('[].radius').optional().description('the radius of the circle, if applicable'),
                        fieldWithPath('[].center').optional().description('the center point of the circle, if applicable'),
                        fieldWithPath('[].pointNorthEast').optional().description('the north-east corner of the rectangle, if applicable'),
                        fieldWithPath('[].pointSouthWest').optional().description('the south-west corner of the rectangle, if applicable')

                )
                        .andWithPrefix('[].center.', pointDescriptorsWithId)
                        .andWithPrefix('[].points[].', pointDescriptorsWithId)
                        .andWithPrefix('[].pointNorthEast.', pointDescriptorsWithId)
                        .andWithPrefix('[].pointSouthWest.', pointDescriptorsWithId)
// ...

This test used to pass in 1.2.1, but fails under 1.2.2 because of this issue.

@wilkinsona wilkinsona added the type: bug A bug label Oct 13, 2017
@wilkinsona wilkinsona added this to the 1.2.3.RELEASE milestone Oct 13, 2017
@wilkinsona wilkinsona changed the title An required field cannot be nested beneath an absent optional field A required field cannot be nested beneath an absent optional field Oct 13, 2017
@wilkinsona
Copy link
Member

I've prototyped something and I think this requirement can be supported in 1.2.x as it doesn't require an API change

@wilkinsona
Copy link
Member

@joemccall86 @ghost When this build has finished, a fix will be available in 1.2.3.BUILD-SNAPSHOT available from https://repo.spring.io/libs-snapshot. If you have a moment, please give it a try and check that it fixes things for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants