OpenAPI schema normalizer – Virtual Assistant job

For this task, I need someone to write a simple class that implements the recursive logic to walk thru a given OpenAPI YAML schema and normalize it.

By normalize, I meant to extract all the inline schemas (the ones defined in place on other schema's properties), put them into the main "components" list, and change the inline schema property to use the $ref reference pointing to the component that was just extracted.

An inline or complex schema type has the following characteristics:

1. The "type" is "object" or;
2. The "properties" contains 1 or more properties

This requirements/steps are:

1. Utilize Microsoft.OpenApi NuGet package (don't try to implement an OpenAPI parser yourself). This package has all the OpenAPI parsing logic. All you need is to instantiate the "OpenApiStreamReader" and read the "OpenApiDocument" from a file. That constains the whole structure of the OpenAPI document.
2. Implement a class that inherith from the "OpenApiVisitorBase" and override the "Visit()" methods as needed.
3. Visit all schema types (components, requests, responses, items, oneOf, etc)
4. When an inline/complex schema is found:
4.1 Get the schema structure and add it to the main "components" where the name of the schema object is the name of the property it originated from
4.2 Replace the old inline/complex schema with the reference ($ref) to the new schema from the "components" list you just created
4.3 Inspect that new schema and see if have an inline/complex type as well. If so, run the same process. (i.e. recursion)
5. Make sure to visit inspect ALL nested inline/complex types, including the ones that you just extracted.

The whole process should navigate thru all the schemas inside the OpenAPI document and normalize it making sure all types are now in the top-level "components" list. Once everything is done, we will have a fully normalized OpenAPI document.

The deliverable:

We need a single class that implement the Visitor pattern, and a Program.cs console app that can test it.

It must work with the 5 OpenAPI schema files attached to this post.

Posted On: April 26, 2024 01:00 UTC
Category: Full Stack Development
Skills:C#, ASP.NET Core, .NET Core, .NET Stack, OpenAPI

Country: Brazil

click to apply

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *