Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add OpenAPI v3.0/v3.1 parser #826

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions chaotic-openapi/chaotic_openapi/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Any, Union, Dict
from openapi_python_client.parser.openapi import GeneratorData
from openapi_python_client.config import Config, ConfigFile, MetaType
from pathlib import Path

def parse(data_dict: Dict[str, Any], config: Config):
return GeneratorData.from_dict(data_dict, config=config)

def config():
return Config.from_sources(ConfigFile(), MetaType.NONE, Path("example.yaml"), 'utf-8', False, None)

from openapi_python_client import _get_document

conf = config()

# print(_get_document(source=conf.document_source, timeout=10))
print(parse(_get_document(source=conf.document_source, timeout=10), conf))
176 changes: 176 additions & 0 deletions chaotic-openapi/chaotic_openapi/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
openapi: 3.0.3
info:
title: Key-Value Storage API
description: An API to manage a key-value store with CRUD operations and basic authentication.
version: 1.0.0
servers:
- url: https://api.kvstore.example.com/v1
description: Production server
- url: http://localhost:3000/v1
description: Development server
components:
securitySchemes:
BasicAuth:
type: http
scheme: basic
schemas:
KeyValuePair:
type: object
properties:
key:
type: string
description: Unique key for the item
value:
type: string
description: Value associated with the key
required:
- key
- value
ErrorResponse:
type: object
properties:
error:
type: string
description: Error message
paths:
/kv:
get:
summary: Get all key-value pairs
description: Retrieve all key-value pairs in the store.
security:
- BasicAuth: []
responses:
'200':
description: A list of key-value pairs
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/KeyValuePair'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
post:
summary: Create a new key-value pair
description: Add a new key-value pair to the store.
security:
- BasicAuth: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/KeyValuePair'
responses:
'201':
description: Key-value pair created successfully
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/kv/{key}:
get:
summary: Get a key-value pair
description: Retrieve a value by its key.
security:
- BasicAuth: []
parameters:
- name: key
in: path
required: true
schema:
type: string
description: The key to retrieve
responses:
'200':
description: Key-value pair retrieved successfully
content:
application/json:
schema:
$ref: '#/components/schemas/KeyValuePair'
'404':
description: Key not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
put:
summary: Update a key-value pair
description: Update the value associated with a specific key.
security:
- BasicAuth: []
parameters:
- name: key
in: path
required: true
schema:
type: string
description: The key to update
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/KeyValuePair'
responses:
'200':
description: Key-value pair updated successfully
'404':
description: Key not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
delete:
summary: Delete a key-value pair
description: Remove a key-value pair from the store.
security:
- BasicAuth: []
parameters:
- name: key
in: path
required: true
schema:
type: string
description: The key to delete
responses:
'204':
description: Key-value pair deleted successfully
'404':
description: Key not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
security:
- BasicAuth: []
Empty file.
7 changes: 7 additions & 0 deletions chaotic-openapi/chaotic_openapi/swagger-schema/contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pydantic import BaseModel
from typing import Optional

class Contact(BaseModel):
name: Optional[str] = None
url: Optional[str] = None
email: Optional[str] = None
7 changes: 7 additions & 0 deletions chaotic-openapi/chaotic_openapi/swagger-schema/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pydantic import BaseModel
from typing import Optional

class Info(BaseModel):
title: str
description: Optional[str] = None
version: str
6 changes: 6 additions & 0 deletions chaotic-openapi/chaotic_openapi/swagger-schema/licencse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic import BaseModel
from typing import Optional

class License(BaseModel):
name: str
url: Optional[str] = None
8 changes: 8 additions & 0 deletions chaotic-openapi/chaotic_openapi/swagger-schema/path_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel
from typing import Optional, List, Dict, Any

class PathItem(BaseModel):
summary: Optional[str] = None
description: Optional[str] = None
parameters: Optional[List[Dict[str, Any]]] = None
responses: Dict[int, Any]
6 changes: 6 additions & 0 deletions chaotic-openapi/chaotic_openapi/swagger-schema/paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic import RootModel
from typing import Optional, List, Dict, Any
from path_item import PathItem

class Paths(RootModel):
root: Dict[str, Dict[str, PathItem]]
24 changes: 24 additions & 0 deletions chaotic-openapi/chaotic_openapi/swagger-schema/swagger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pydantic import BaseModel, Field
from typing import Any, Dict, List, Optional
import json, yaml

from info import Info
from paths import Paths

class SwaggerSchema(BaseModel):
swagger: str = Field("2.0", literal=True)
info: Info
host: Optional[str] = None
basePath: Optional[str] = None
schemes: Optional[List[str]] = None
consumes: Optional[List[str]] = None
produces: Optional[List[str]] = None
paths: Paths

def parse_swagger_file(file_path: str) -> SwaggerSchema:
with open(file_path, "r") as file:
data = yaml.load(file)
return SwaggerSchema(**data)

parsed_schema = parse_swagger_file("swagger_example.yaml")
print(parsed_schema)
Loading