Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Conversation

@moisesventura
Copy link

The current buildLooksLike code verifies the document to deserialize contains all the properties (required and non-required) from the RAML object declaration. This causes the object identification (and thereby the deserialization) to fail if any of the non-required properties is missing from the response document.

By adding the following code only the mandatory properties are verified:

List<TypeDeclaration> mandatoryProperties = otd.properties().stream().filter(propertyTypeDeclaration -> propertyTypeDeclaration.required())
					.collect(Collectors.toList());

This results into code like the following (with no optional attributes in the containsAll):

    private boolean looksLikeMortgageAccount(Map<String, Object> map) {
      return map.keySet().containsAll(Arrays.asList("accountId","type")) && map.get("type").equals("MORTGAGE");
    }

Apologies for the automated code formatting.

@jpbelang jpbelang changed the base branch from master to release/1.0.7 January 19, 2020 17:45
@jpbelang
Copy link
Contributor

I've checked the PR and the problem is real, but the solution has a problem: you can't just check the mandatory fields, since the non-mandatory fields might also provide information into the unioned type. For example:

types:
   First:
      name: string
   Second:
      name: string
      color:  string
         required: false
   TheirUnion:
        type: First |Second
        example:
            name: Paul
            color: blue

The example should deserialize to type Second. Using just the mandatory types would deserialize to First.

I'll see what I can do, but I'm booked for the weekend. Does your patch satisfy your immediate need ?

@moisesventura
Copy link
Author

I see the problem, but I don't know how non-mandatory fields can determine a union type. In your example, an object that does not have the optional color, is a First object, or a Second object that just doesn't have the optional attribute? Maybe Second should define Color as mandatory, to be able to tell them apart.

The suggested code covers the discriminators, so that can be used to discern one object from another. The resulting code looks like (only using mandatory fields):

return map.keySet().containsAll(Arrays.asList("accountId","type")) && map.get("type").equals("MortgageAccount");

The current patch works fine with our definition and code. I've created an snapshot version and I'm successfully using it from our local Nexus server.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants