Search code examples
c#restsabre

Generating C# model classes from JSON Schema for Sabre REST API


We are implementing Sabre REST API to access air, hotel and vehicle content in our C# ASP.NET MVC application.

So far we have hit some issues when generating C# model classes from the JSON Schema documents provided by Sabre Dev Studio. We are using Quicktype CLI (https://quicktype.io/) to generate the model classes.

Steps we have followed so far:

  1. Downloaded OTA_AirLowFareSearchRQ.jsonschema and OTA_AirLowFareSearchRS.jsonschema files into a folder.
  2. Using PowerShell navigated to the folder and invoke quicktype -s schema OTA_AirLowFareSearchRQ.jsonschema OTA_AirLowFareSearchRS.jsonschema -o SabreAirLowFareSearch.cs

Unfortunately it fails with the message "Error: Trying to make an empty union - do you have an impossible type in your schema?". This appears to be isolated to OTA_AirLowFareSearchRQ.jsonschema.

Is there a tool or best practice to successfully generate C# model classes from the JSON Schema documents?


Solution

  • I've imported the NuGet Packages:

    • NJsonSchema
    • NJsonSchema.CodeGeneration
    • NJsonSchema.CodeGeneration.CSharp

    And done the code below:

    using System;
    using System.Threading.Tasks;
    using NJsonSchema;
    using NJsonSchema.CodeGeneration.CSharp;
    
    namespace RunningTestings
    {
        class Program
        {
            static void Main(string[] args)
            {
                CreateClassfromJsonSchema(@"http://files.developer.sabre.com/doc/providerdoc/STPS/bfm/v410/OTA_AirLowFareSearchRQ.jsonschema").Wait();
            }
    
            public static async Task CreateClassfromJsonSchema(string url)
            {
                JsonSchema jsonSchema = await JsonSchema.FromUrlAsync(url);
                CSharpGenerator generator = new CSharpGenerator(jsonSchema);
                string file = generator.GenerateFile();
            }
        }
    }
    

    Which generated the code attached on this link: https://file.io/LlRNsf (I'm not sure how long it will last there)

    You need to perform another call for the response.