microsoft / kiota-serialization-json-ruby

Kiota serialization provider implementation for Ruby and JSON
https://aka.ms/kiota/docs
MIT License
1 stars 4 forks source link

Deserialization of attributes with nil values does not work if an object is expected #8

Closed sundling closed 1 year ago

sundling commented 1 year ago

Requesting events via https://graph.microsoft.com/v1.0/users/{$user-id}/calendar/events will return events in the format of:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('$user-id')/calendar/events",
  "value": [
    {
      ...
      "recurrence": null,
      ...
      transactionId: null
    }
  ]
}

recurrence is allowed to be nil but will be an object if it is a recurring event. See https://github.com/microsoftgraph/msgraph-sdk-ruby/blob/abc8251e203cddedd818d52dfbcaf4657c90d235/lib/models/event.rb#L308

An exception is raised from https://github.com/microsoft/kiota-serialization-json-ruby/blob/6b0b1879f06e099e1e47f3a1dacc4b527a09e13d/lib/microsoft_kiota_serialization_json/json_parse_node.rb#L100 since the variable @current_node will be nil when parsing the JSON object above.

The error will be solved by modifying the code in a similar way as https://github.com/microsoft/kiota-java/blob/04fe9dfefae7b754057958f214e69a02e07c6645/components/serialization/json/src/main/java/com/microsoft/kiota/serialization/JsonParseNode.java#L301-L302

It could be as simple as adding a check for

next if v.nil?

above this line: https://github.com/microsoft/kiota-serialization-json-ruby/blob/6b0b1879f06e099e1e47f3a1dacc4b527a09e13d/lib/microsoft_kiota_serialization_json/json_parse_node.rb#L101

This has been solved by many of the other serialization providers, using slightly different methods.

The fix above will also change how values that are not objects will be represented after parsing. Some primitive values are currently being mutated, like the transactionId in the example which is cast to an empty string. This happens for booleans and numbers as well.

j15e commented 1 year ago

I confirm this is very problematic, it also makes it impossible to deserialize any payload where a date is null. This makes it impossible to get groups payload because most of the time the deleted_date_time attribute will be null.

Example of a correct 200 OK response raising Error during deserialization:

Capture d’écran, le 2023-08-16 à 11 46 31

I'll try getting around this by selecting only the attributes I need.

j15e commented 1 year ago

Here is a sample script on how to select only the attributes you want since they is no Ruby SDK guide yet:

  group_id = "0beecd7e-aeb6-454a-862f-d32e1fcf85e2"
  group_params = MicrosoftGraph::Groups::GroupsRequestBuilder::GroupsRequestBuilderGetQueryParameters.new
  group_params.select = ["displayName"]
  request_configuration = MicrosoftKiotaAbstractions::RequestConfiguration.new
  request_configuration.query_parameters = group_params

  client.groups.by_group_id(group_id).get(request_configuration).resume

This way you may avoid the Error during deserialization (if you do not ever need the information from those attributes).