Bishwas-py / djapy

No bullshit, Django Rest API Framework
https://djapy.io
54 stars 3 forks source link

Add `raise MessageOut` schemas in OpenAPI schemas. #13

Open Bishwas-py opened 3 months ago

Bishwas-py commented 3 months ago

Current it's

class MessageOut(Exception):
    message = None
    alias = None
    message_type = None

    def __init__(self, message, alias, message_type):
        self.message = message
        self.alias = alias
        self.message_type = message_type

so we want a feature that would add the attributable schemas of MessageOut to OpenAPI, and it should be able to detect schemas from every_inside_funtion.

def get_by_id(todo_id):
    try:
        todo_item = TodoItem.objects.get(id=todo_id)
        return todo_item
    except:
        raise MessageOut(
            "Todo item not found",
            "todo_item_not_found",
            "error"
        )

@djapify
def get_todo_item(request, todo_id: int) -> TodoItemSchema:
    todo_item = get_by_id(todo_id)

    if todo_item.is_owner(request.user):
        return todo_item
    raise MessageOut(
        "You are not allowed to view this item",
        "todo_item_not_found",
        "error"
    )

possible approach, using inspect API to get all raises and comparing them with a base class, and assigning it to the schema.

Bishwas-py commented 3 months ago

Possible code is already written here, in django-todo example.