Amazon-SP-API-CSharp icon indicating copy to clipboard operation
Amazon-SP-API-CSharp copied to clipboard

How hard is it to get rid of RestSharp and NewtonsoftJson?

Open GuybrushX opened this issue 2 years ago • 5 comments

RestSharp seems to work fine so far but the more I'm using RestSharp the more trouble it creates in my experience. Trouble in the sense of breaking changes after updates or that in some cases it tries to be smart and it adds headers which you can't remove etc. I had cases where I had to switch to HttpClient because of that already (or I was unable to configure RestSharp appropriately which is possible, of course).

Since .Net includes HttpClient and also System.Text.Json I wonder how difficult it would be to get rid of RestSharp and also NewtonsoftJson and remove this dependencies? The less dependencies the better :-)

RestSharp is also using HttpClient behind the scenes already.

Checking the code it shouldn't be too hard but I'm curious about the opinion of others. Maybe I'm overseeing something or the code generation from the Amazon SP-API does somehow depend on it etc. Maybe it's just a "never touch a running system" thing?

Any thoughts?

GuybrushX avatar Sep 08 '23 10:09 GuybrushX

I am keen to know as well.

I had to downgrade RestSharp and NewtonsoftJson for my legacy project to work.

Newtonsoft.Json 13.0.1 RestSharp 106.15.0 RestSharp.Serializers.NewtonsoftJson 106.15.0

mrkevinhsiao avatar Sep 09 '23 04:09 mrkevinhsiao

Agree, it would be nice to remove the dependencies. Microsoft Reference on migrating

paul-van-der-hoeven avatar Oct 30 '23 14:10 paul-van-der-hoeven

We also have interest in this.

TechyChap avatar Oct 31 '23 15:10 TechyChap

Newtonsoft.Json

After digging around in the code here are some findings:

  • Searching for "[Json" shows 1099 results
  • 904 changes are in the "Models" folder -> Many are auto-generated by swagger-codegen and using JsonConstructorAttribute which is also available in System.Text.Json so not a big deal
  • 415 JsonProperty attributes -> most can be migrated by a simple search & replace with JsonPropertyName but a few are using either the StringEnumConverter or a custom converter. Looks like there is JsonStringEnumConverter which should work.

@paul-van-der-hoeven Thank you for the link, was quite useful to quickly find what is supported and what is not 👍

Not sure how all the Models are kept in sync with Amazon:

  • is this a manual process?
  • does it make sense to migrate to OpenAPI Generator which seems to have support for System.Text.Json and (experimental) HttpClient? I've not used either so this needs some research/confirmation from someone who has more experience.

RestSharp

I didn't dig too deep into the RestSharp part yet but I successfully migrated the LWAClient to use HttpClient and System.Text.Json to generate an access token.

Quick search for RestSharp returns 180 results whereas 164 are calls to CreateAuthorizedRequestAsync.

Conclusion

Technically it should be doable. It just needs very careful adjustments to hundreds of places.

My main question though is to @abuzuhri: Is it even an option to get rid of those dependencies from your side?

GuybrushX avatar Nov 01 '23 08:11 GuybrushX

Its possible but will take some times

abuzuhri avatar Nov 01 '23 08:11 abuzuhri