PokeApiNet icon indicating copy to clipboard operation
PokeApiNet copied to clipboard

JsonSerializationException thrown in PokeClient.GetResourceAsync<Pokemon> for some (mostly Hisuian) Pokémon.

Open Towodile opened this issue 2 years ago • 11 comments

Error converting value {null} to type 'System.Int32'. Path 'base_experience', line 1, position 354.

This probably is caused by the fact these Pokémon have a "base_experience" with value null in JSON, which cannot directly be converted to Int32 in C#. Making Pokemon.BaseExperience nullable will fix it.

To recreate:

Pokemon wyrdeer = await pokeClient.GetResourceAsync<Pokemon>("wyrdeer");

or use any other pokemon where "base_experience": null.

Towodile avatar Jul 13 '22 18:07 Towodile

Thanks for reporting this. Would you like to submit a PR with a fix?

jtwotimes avatar Jul 14 '22 00:07 jtwotimes

I just noticed the very same problem. If you can find the time, I would greatly appreciate it if you could release a package with the base experience fix.

FelixDamrau avatar Aug 15 '22 20:08 FelixDamrau

So sorry for the delay - just published version 3.0.6 with this fix out to nuget that is available now.

jtwotimes avatar Aug 15 '22 23:08 jtwotimes

Thank you for the quick reply! I didn't mean to rush you. I hope I was not rude.

FelixDamrau avatar Aug 16 '22 15:08 FelixDamrau

Unfortunately I have to bug you again :/

I think something went wrong with the nuget push. The nuget version 3.0.6 seems to still contain the 3.0.5 assembly version and therefore there is still the exception when the serializer tries to parse null into an integer.

See: https://nuget.info/packages/PokeApiNet/3.0.6 image

I checked the file hashes, the 3.0.6 and 3.0.5 dlls seem the be the same. image

FelixDamrau avatar Aug 16 '22 17:08 FelixDamrau

That's what I get for not doing a dotnet clean beforehand, lol. I just published version 3.0.7 out - this should one has the fix it in. Sorry about that!

jtwotimes avatar Aug 17 '22 02:08 jtwotimes

Thanks a bunch! The new version is correct now.

Unfortunately there is another issue. :'( Now base_happiness of PokemonSpeciesis causing trouble. It's the same problem when the JsonSerializer tries to parse null into an value type.

I'll do some testing. I'll write an integration test that just gets the Pokemon-Types from the PokeApi.co-API and I'll fix all issues that may be there. Do you want to keep the diskussion in this issue? If not I'll make a new issue with my test results. Unfortunately, I will not be able to finish/start my testing today.

FelixDamrau avatar Aug 17 '22 18:08 FelixDamrau

I went through and tested all serialization for all endpoints and I believe I've addressed all of these similar issues in the just-released version 3.0.8.

jtwotimes avatar Aug 18 '22 12:08 jtwotimes

I ran my test as well.

I found two different issues.

  1. base_happiness may be null (The same issue) [EDIT: solved with version 3.0.8] These are the pokemon that cause issue
❌ wyrdeer - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
❌ kleavor - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
❌ ursaluna - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
❌ basculegion - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
❌ sneasler - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
❌ overqwil - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
❌ enamorus - Error converting value {null} to type 'System.Int32'. Path 'base_happiness', line 1, position 22.
  1. The language-endpoint uses case-senstive and not "all lower case" identifier strings
❌ ja-Hrkt - Response status code does not indicate success: 404 (Not Found).
❌ zh-Hant - Response status code does not indicate success: 404 (Not Found).
❌ zh-Hans - Response status code does not indicate success: 404 (Not Found).
❌ pt-BR - Response status code does not indicate success: 404 (Not Found).

This is an issue, because the PokeApiClient (Line 113) converts all letters to lower-case. That's usually fine, however these four languages require upper case letters.

So a request

http GET https://pokeapi.co/api/v2/language/ja-Hrkt Accept:'*/*'
# Status: 200 OK Size: 634 Bytes

gets converted to

http GET https://pokeapi.co/api/v2/language/ja-hrkt Accept:'*/*'
# Status: 404 Not Found Size: 9 Bytes

The fix for the 2nd issue isn't that staight forward.

  • Removing the ToLower completely would be a breaking change
  • Adding an exception for the apiEndpoint language seems to be a bit iffy

I'll run my tests vs version 3.0.8 and post my results here as well.

FelixDamrau avatar Aug 18 '22 18:08 FelixDamrau

Version 3.0.8 solved the nullable issue. Thanks a bunch for the quick fix. <3

The problem with the language endpoint is still there - but that's a completely different issue.

FelixDamrau avatar Aug 18 '22 19:08 FelixDamrau

@FelixDamrau issue with language endpoint should be fixed in latest version

ondrejmyska avatar Oct 25 '22 16:10 ondrejmyska

I just saw that I didn't reply here... 🤦‍♀️ Everything works fine for me now. Thanks a bunch! <3

FelixDamrau avatar Jun 04 '23 13:06 FelixDamrau