Swagger error?

Hey there. My Visual Studio doesn't eat your https://api.youneedabudget.com/papi/spec-v1-swagger.json

[Fatal]Error parsing swagger file. Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Nullable`1[Microsoft.Rest.Modeler.Swagger.Model.DataType]' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'properties.last_modified_on.type', line 16, position 15.

I'd say that maybe I'm doing something wrong, but there's not a lot to do - enter the link, press "Go".

10replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Some of our types are specified as nullable in the spec and this might be giving the Swagger parser you are using trouble.  For example, some of our types look like this:

    "type": ["string", "null"]

    If you try to change this to:

    "type": "string"

    and it works, then that would explain it.  The Swagger 2.0 spec has limited and inconsistent support for nullable types and some parsers do not understand this format.  We will eventually migrate to OpenAPI 3.0 spec which has full support for nullable types.  In the meantime, you may be able to use an alternative parser or transform the spec on your side before parsing it.

    Reply Like
    • Brady Eh, I'll manage without it. Helpers make you spoiled :) For my app I need only a small portion of API, it's easier to implement it myself. Just wanted to see what Swagger will produce, and it didn't.

       

      I'm using the implementation that's built-in latest Visual Studio, and I'm developing a Xamarin.Android app, if you would like to research something and change things on your end. I mean, since it's built-in a lot of beginner developers would probably try to use it, so you'll probably want it to work without making them fiddle around looking for solutions. Or not. Just a thought.

      Reply Like 1
  • I ran into a similar issue as well, except my parser just ignored it and now I don't have memos on my transactions. I'm using the official swagger codegen.

    I'm not an expert on swagger, but I believe the YNAB definition file is incorrect.

    From https://swagger.io/docs/specification/data-models/data-types/: type takes a single value. type as a list is not valid in OpenAPI (even though it is valid in JSON Schema)

    And from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject: type - Value MUST be a string. Multiple types via an array are not supported.

    Reply Like
    • If anyone else is having this problem, I put together the following bit of bash to modify the JSON on the fly:

      curl -s "https://api.youneedabudget.com/papi/spec-v1-swagger.json" | jq -c . | sed -r 's/"type":\["([a-zA-Z]+)"(,"[a-zA-Z]+")+\]/"type":"\1"/g' | jq .
      Reply Like
  • FYI - I am working on a change to remove these nullable types from our spec file to prevent the issues some parsers have with it.  You can follow our Release Notes for information when this change is made. Thanks for letting us know!

    Reply Like 1
  • Yesterday we released the updated spec that removes these nullable types so that the spec should be fully compliant with the Swagger 2.0 spec now.

    Reply Like
      • chbndrhnns
      • Cloud Test Engineer
      • Magenta_Deer_43f0695638
      • 1 yr ago
      • Reported - view

      Brady I feel like with your recent changes some things broke. I generated a swagger client for Python and cannot even get a list of accounts as the 'note' field must not be None according to the client.

       

      The same is true here for creating transactions: a flag_color must be given and None/"null" is no longer acceptable to the client.

       

      >>> api_response = api_instance.get_accounts(budget)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api/accounts_api.py", line 158, in get_accounts
          (data) = self.get_accounts_with_http_info(budget_id, **kwargs)  # noqa: E501
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api/accounts_api.py", line 232, in get_accounts_with_http_info
          collection_formats=collection_formats)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 322, in call_api
          _preload_content, _request_timeout)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 161, in __call_api
          return_data = self.deserialize(response_data, response_type)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 233, in deserialize
          return self.__deserialize(data, response_type)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 272, in __deserialize
          return self.__deserialize_model(data, klass)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 613, in __deserialize_model
          kwargs[attr] = self.__deserialize(value, attr_type)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 272, in __deserialize
          return self.__deserialize_model(data, klass)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 613, in __deserialize_model
          kwargs[attr] = self.__deserialize(value, attr_type)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 250, in __deserialize
          for sub_data in data]
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 250, in <listcomp>
          for sub_data in data]
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 272, in __deserialize
          return self.__deserialize_model(data, klass)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/api_client.py", line 615, in __deserialize_model
          instance = klass(**kwargs)
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/models/account.py", line 79, in __init__
          self.note = note
        File "/Users/jo/.virtualenvs/mm2ofx-PGQgrc8L/src/ynab-client/swagger_client/models/account.py", line 231, in note
          raise ValueError("Invalid value for `note`, must not be `None`")  # noqa: E501
      ValueError: Invalid value for `note`, must not be `None`
      Reply Like
    • chbndrhnns

      That's strange.  Ok, first off, I generated a diff of the old/new version so you can see clearly what was changed.  Here it is: https://www.diffchecker.com/1h1hZlZi.  Notice, most of the types were changed from something like this: "type": ["string", "null"] to this: "type": "string".

      As mentioned above, Swagger 2.0 spec knows nothing of null for types and our spec was previously not compliant.

      But, obviously something broke on your end. Are you using Swagger codegen to do the generation? If so, are you using the latest version? I saw what looks to be a related issue here: https://github.com/swagger-api/swagger-codegen/issues/6291 which might help. Also, are the errors you seeing from your tests? 

      2 quick things to get you back up and running would be to continue using the old version of the spec (new versions will be backwards compatible) or to do a generation-time conversion of the spec to restore these nullable types.  Neither of these are ideal, obviously, but could be stop-gap solutions.

      As for the second option, if you have the ability to use sed, you could download the spec and then run this command to convert the types back to "nullable", with this (works on MacOS):

      sed -E -i '' 's/\: \"(string|number|integer|array|boolean)\"+/"\: [\"\1\",\"null\"]/g' spec-v1-swagger.json

      Then, you could run the generator with this modified file.

      I hope this helps.  Please let me know how it goes.

      Reply Like
      • chbndrhnns
      • Cloud Test Engineer
      • Magenta_Deer_43f0695638
      • 1 yr ago
      • Reported - view

      Brady I did some further tests. Thanks so much for the sed hack. Applying it results in a usable swagger-client for Python, again.

      I was always using the latest version of swagger-codegen (2.3.1).

      A comparison of the branches can be found at [1]. The were created using the Makefile targets `client` and `client-allow-null`.

      So with null not allowed, a simple API call like retrieving accounts fails with 

        File "/Users/jo/dev/bank2budget/ynab-client/swagger_client/models/account.py", line 79, in __init__
          self.note = note
        File "/Users/jo/dev/bank2budget/ynab-client/swagger_client/models/account.py", line 231, in note
          raise ValueError("Invalid value for `note`, must not be `None`")  # noqa: E501

      Getting a transaction fails with 

        File "/Users/jo/dev/bank2budget/ynab-client/swagger_client/models/transaction_detail.py", line 104, in __init__
          self.flag_color = flag_color
        File "/Users/jo/dev/bank2budget/ynab-client/swagger_client/models/transaction_detail.py", line 287, in flag_color
          raise ValueError("Invalid value for `flag_color`, must not be `None`")  # noqa: E501
      ValueError: Invalid value for `flag_color`, must not be `None`

       

      I do not understand the generation process and swagger well enough to understand what is going on here. I find it strange and confusing, though.

       

      [1] https://github.com/chbndrhnns/ynab_client/compare/without-sed...2018-09-17?expand=1

      Reply Like
    • chbndrhnns 😅 I'm glad you're up and running again.  I have found the Swagger 2.0 codegen tooling to be quite rough in certain areas and this looks to be one of them.  I think there are quite a few assumptions being made that end up causing issues.

      Reply Like 1
Like1 Follow
  • 1 Likes
  • 1 yr agoLast active
  • 10Replies
  • 835Views
  • 4 Following