Is there a Python 3 package or code snippet that can read an OpenAPI 3 YAML file and convert it to a Python dictionary?
This is an example of the contents in an OpenAPI 3 YAML file
openapi: 3.0.0
servers:
url: https://localhost/api
paths:
/user/{userId}:
get:
summary: Get a user by ID
parameters:
- in: path
name: userId
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
$ref: '#/components/schemas/UserFullName'
age:
type: integer
UserFullName:
type: object
properties:
firstName:
type: string
lastName:
type: string
I want the package/code to generate a Python dictionary looking something like this
{
"/user/{userId}": {
"get": {
"200": {
"id": "integer",
"name": {
"firstName": "string",
"lastName": "string"
}
"age" "integer"
}
}
}
}
------ Edit -----
I have tried PyYaml, however, it is not returning what I am expecting.
I am expecting the '$ref' key-value pair to act as a string interpolation like this:
{
"id":0,
"name":{
"firstName":"string",
"lastName":"string"
},
"age":0
}
Instead, PyYaml is returning this:
{
"id":0,
"name": {
"$ref": "#/components/schemas/UserFullName"
},
"age":0
}