1

I'm fighting with the converting json schema from Fedex Ship API to c# objects, which can be downloaded here: https://developer.fedex.com/api/en-us/catalog/ship/docs.html

I tried some online tools, NJsonSchema.CodeGeneration and also many topics here like this: Generate C# classes from JSON Schema but actually nothing worked for me. I noticed, the Fedex schema doesn't look like example json schema in many topics. I'm not sure if I'm missing something, or why do they have different schema? Because then it's not possible to convert it in usual way.

At the end I had to withdraw "schemas" part from their json schema and finally I was able to create c# objects. However, they were still somehow not valid -> it was crashing when I tried to parse Response. My final solution was to manually get response result and I converted it as clean json (not json schema anymore) to c# objects.

I would like to know, if I'm doing something wrong, or if exists some better approach, how to get c# objects from their json schema.

zabuli
  • 96
  • 1
  • 7
  • 1
    That looks like a Swagger/OpenAPI definition. You may try the [Swagger Codegen](https://swagger.io/tools/swagger-codegen/) if it creates a client that fits your needs. Or if you at least can reuse the created model classes. – Ralf Jun 12 '23 at 09:19
  • If you go to the Fedex Website, and look at the "Request Sample" Area in the API section, you can actually see the response json for the various response codes. (Also for other languages than C#) You can create a C# object for the response objects as such. Also this shows how Fedex intends for you to send them requests. (Auto generated client or not.) Fedex recommends an interface IRestResponse type, that contains the contract values you require. – Morten Bork Jun 12 '23 at 09:20
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 12 '23 at 12:09
  • @Ralf thank you very much, I didn't know about this. Fedex has still invalid json schema, but when I removed "schemes" part, it started to be valid in Swagger editor. In Swager Hub it's also possible to get full objects structure in multiple programming languages. – zabuli Jun 12 '23 at 14:18

1 Answers1

0

Based on comment below my question I found out in official Microsoft site, there are 5 official approaches how to get c# object from open api json schema: https://learn.microsoft.com/sk-sk/aspnet/core/tutorials/getting-started-with-nswag?view=aspnetcore-2.0&tabs=visual-studio#code-generation-1

In our project we decided for approach using connected service in Visual Studio. I put given json schema from FedEx to our solution and then linked the path to that file into connected service.

enter image description here

zabuli
  • 96
  • 1
  • 7