Skip to content

GettingStarted

Darrel edited this page Jun 27, 2021 · 3 revisions

The OpenAPI.NET library is a document object model (DOM) for an OpenAPI description document. It provides a set of classes for constructing semantically valid descriptions of HTTP APIs. The library also includes a writer for serializing the DOM into JSON or YAML in either OpenAPI v3 or V2 format.

Creating an OpenAPI Document

vardocument=newOpenApiDocument{Info=newOpenApiInfo{Version="1.0.0",Title="Swagger Petstore (Simple)",},Servers=newList<OpenApiServer>{newOpenApiServer{Url="http://petstore.swagger.io/api"}},Paths=newOpenApiPaths{["/pets"]=newOpenApiPathItem{Operations=newDictionary<OperationType,OpenApiOperation>{[OperationType.Get]=newOpenApiOperation{Description="Returns all pets from the system that the user has access to",Responses=newOpenApiResponses{["200"]=newOpenApiResponse{Description="OK"}}}}}}};

Reading and writing a OpenAPI description

// using Microsoft.OpenApi.Extensions; // Required for the .Serialize methodvarhttpClient=newHttpClient{BaseAddress=newUri("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/")};varstream=awaithttpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml");// Read V3 as YAMLvaropenApiDocument=newOpenApiStreamReader().Read(stream,outvardiagnostic);// Write V2 as JSONvaroutputString=openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0,OpenApiFormat.Json);

Clone this wiki locally