[Feature]: Support for injecting custom input in custom openapi error response format
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 38/100
Rechercherichtung
Start with FlaskOpenAPIErrorsHandler and compare its behavior around commit 22ace6d, using the custom handler in server.py as the reproduction. Read test.yaml to understand the validation request and response, then verify that custom injected fields and OpenAPI-Core error details can be returned together in the current release.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Suggested Behavior
I would like to add a way to customize error response format and would like to add additional information in the error response generated by OpenAPI-Core
currently the error response generated by OpenAPI-Core is as follows
{
"errors": [
{
"class": "<class 'openapi_core.validation.schemas.exceptions.InvalidSchemaValue'>",
"status": 400,
"title": "Value {'flag': 'testF'} not valid for schema of type object: (<ValidationError: \"'testF' is too short\">,)"
}
]
}
I want to customize the response in following format
{
"title": "Failed to execute your favorite API" \\ Custom String provided by code
"causedBy": [
"Value {'flag': 'testF'} not valid for schema of type object: (<ValidationError: \"'testF' is too short\" \\ Error Resposne generated by OpenAPI Core
]
}
As you can see in the comments, the message in causedBy field includes error messages generated by OpenAPI Core module
whereas the message in title field includes data generated and injected by actual code
Why is this needed?
This is required to make OpenAPI core more flexible and customizable and become a better choice for openapi linters and validators.
Also If OpenAPI does not support cutomization on the content of response, then it would be a deal breaker for most of the implementation as most of the projects tries to maintain some standard format of response
References
I came across FlaskOpenAPIErrorsHandler class and created a custom handler class which inherits from FlaskOpenAPIErrorsHandler class
I used constructor of this class to inject custom fields and generate custom response accordingly.
following is my Flask code server.py
#!/usr/bin/python3
"""Test server."""
from flask import Flask, request, jsonify
from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator, FlaskOpenAPIErrorsHandler
from openapi_core import Spec
# Custom Error Handler block
class MyCustomErrorHandler(FlaskOpenAPIErrorsHandler):
""""Custom Error Handler"""
def __init__(self, title:str):
self.title = title
def handle(self, errors:list):
response_object = jsonify({
"title": self.title,
"causedBy" : [self.handle_error(error) for error in errors]
})
response_object.error_code = 400
return response_object
def handle_error(self, error):
"""
Converts error object into error string message
:param error: Error object which stores exception message
:type error: Exception object
:return: Error message string corresponding to error object
:rtype: str
"""
if error.__cause__ is not None:
error = error.__cause__
return str(error)
SPEC = "test.yaml"
app = Flask(__name__)
spec_object = Spec.from_file_path(SPEC)
my_error_handler_object = MyCustomErrorHandler("Failed to execute test API")
openapi_decorator = FlaskOpenAPIViewDecorator.from_spec(spec_object, openapi_errors_handler=my_error_handler_object)
@app.route("/test", methods=["POST"])
@openapi_decorator
def read_permission():
"""Test function"""
temp = jsonify({
"stri_normal_json": request.json.get("flag", 1)
})
print(dir(temp))
return temp
if __name__ == "__main__":
app.run(host="0.0.0.0", port=345, debug=True)
#Valid request: curl -X POST http://localhost:345/test --data '{"flag":"rohana"}' -H 'Content-Type: application/json'
#Invalid Request: curl -X POST http://localhost:345/test --data '{"flag":"rohan"}' -H 'Content-Type: application/json'
Following is OpenAPI Spec
test.yaml
openapi: '3.0.2'
info:
title: Test Title
version: '1.0'
servers:
- url: http://localhost:345/
paths:
/test:
post:
requestBody:
content:
application/json:
schema:
type: object
required:
- flag
properties:
flag:
x-pii: true
type: string
pattern: "^[\\w.-]*$"
minLength: 6
maxLength: 20
responses:
200:
description: Sample response
content:
application/json:
schema:
type: object
properties:
stri_normal_json:
type: string
minLength: 6
maxLength: 20
As we can see in code, I am creating object of custom error handler and injecting custom string as part of constructor in above example following is the line
my_error_handler_object = MyCustomErrorHandler("Failed to execute test API")
Following is the output of curl request
Invalid Curl Request
root@ip-10-31-1-220:~/playground# curl -X POST http://localhost:345/test --data '{"flag":"rohan"}' -H 'Content-Type: application/json'
{
"causedBy": [
"Value {'flag': 'rohan'} not valid for schema of type object: (<ValidationError: \"'rohan' is too short\">,)"
],
"title": "Failed to execute test API"
}
Valid Curl Request
root@ip-10-31-1-220:~/playground# curl -X POST http://localhost:345/test --data '{"flag":"rohana"}' -H 'Content-Type: application/json'
{
"stri_normal_json": "rohana"
}
In this way I was able to customize the response body and also inject my custom string when I create object of custom error handler. which was working fine till OpenAPI 0.16.x but it started failing since OpenAPI 0.18.x
this issue happened due to following pull request
https://github.com/python-openapi/openapi-core/commit/22ace6d39607ef769ba0a36e8a24dc6df704a620
Would you like to implement a feature?
Yes
- Vorherrschende Sprache
- Python
- Sterne
- 368
- Forks
- 140
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python-openapi/openapi-core
-
kind/bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
python-openapi/openapi-core#1188 · 2 Kommentare ·
-
kind/bug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
python-openapi/openapi-core#1225 · 2 Kommentare ·
-
kind/bug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 76/100
python-openapi/openapi-core#1212 ·
-
[Bug]: Query parameter validation fails to match empty string when listed as a valid enum value Offenkind/bug
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
python-openapi/openapi-core#1210 ·
-
kind/bug kind/bug/confirmed
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 58/100
python-openapi/openapi-core#1180 · 3 Kommentare ·
Alle Issues in python-openapi/openapi-core
Ähnliche Issues
-
enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
canonical/paas-charm#368 · 1 Kommentar ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
tech debt
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
StevenBlack/hosts#3256 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
qualcomm/qai-appbuilder#275 ·