Training
Module
Improve the developer experience of an API with Swagger documentation - Training
Learn how to document an existing API, written in C#/ASP.NET Core, using Swashbuckle, Swagger/OpenAPI, and Swagger UI.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
OpenAPI.NET has the ability to convert OpenAPI documents between different formats, such as YAML or JSON. This capability can be useful when you need to share or collaborate on an OpenAPI document with others who prefer a different format.
Here's an example of how you can convert an OpenAPI document from YAML to JSON using OpenAPI.NET.
This example converts the OpenAPI document created and modified in the previous examples from YAML to JSON.
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Writers;
// Load the existing OpenAPI document from a YAML file
using var streamReader = new StreamReader("updated-pet-store.yaml");
var reader = new OpenApiStreamReader();
var document = reader.Read(streamReader.BaseStream, out var diagnostic);
// Serialize and save the OpenAPI document to a JSON file
using var streamWriter = new StreamWriter("updated-pet-store.json");
var writer = new OpenApiJsonWriter(streamWriter);
document.SerializeAsV3(writer);
Console.WriteLine("OpenAPI document converted from YAML to JSON.");
Compare the original YAML with the new JSON.
openapi: 3.0.1
info:
title: PetStore API
version: 1.0.0
servers:
- url: https://api.petstore.com
paths:
/pets:
get:
description: Get all pets
responses:
'200':
description: A list of pets
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
/pets/post:
post:
description: Add a new pet
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
required: true
responses:
'201':
description: Pet created successfully
components:
schemas:
Pet:
type: object
properties:
name:
type: string
category:
type: object
properties:
id:
type: integer
name:
type: string
{
"openapi": "3.0.1",
"info": {
"title": "PetStore API",
"version": "1.0.0"
},
"servers": [
{
"url": "https://api.petstore.com"
}
],
"paths": {
"/pets": {
"get": {
"description": "Get all pets",
"responses": {
"200": {
"description": "A list of pets",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
}
}
}
}
}
}
},
"/pets/post": {
"post": {
"description": "Add a new pet",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
},
"required": true
},
"responses": {
"201": {
"description": "Pet created successfully"
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
}
}
}
}
}
Training
Module
Improve the developer experience of an API with Swagger documentation - Training
Learn how to document an existing API, written in C#/ASP.NET Core, using Swashbuckle, Swagger/OpenAPI, and Swagger UI.
Documentation
Learn how to use the OpenAPI.NET library to modify an OpenAPI document.
Learn how to use the OpenAPI.NET library to create an OpenAPI document.
Learn about the features and use cases for the OpenAPI.NET library.