You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I write an app in django using the django rest framework and pydantic classes as well as spectacular extended_schema annotations.
when exporting views that take types having optional fields the export does not validate:
classRequestBSchema(BaseModel):
"Pydantic class request string to be send to service"string: strtoggle: Optional[bool] =True
neither do classes of type
classRequestASchema(BaseModel):
"Pydantic class request string to be send to service"string_pairs: List[Tuple[str, str]]
To Reproduce
models/service.py
fromtypingimportAnyfromtypingimportDictfromtypingimportOptionalfrompydanticimportBaseModelfrompydanticimportFieldclassRequestSchema(BaseModel):
"Pydantic class request string to be send to service"text: str=Field(..., min_length=1)
toggle: Optional[bool] =TrueclassResponseSchema(BaseModel):
"Pydantic class defining result to be received from service"result: Dict[str, Any]
classErrorResponseSchema(BaseModel):
detail: str# A human-readable error messagecode: str# An error code (e.g., VALIDATION_ERROR)
services/service.py
from ..modelsimportRequestSchemafrom ..modelsimportResponseSchemaclassService:
defservice(self, query: RequestSchema) ->ResponseSchema:
"""do stuff."""result=query.text,
returnResponseSchema(result=result)
views/service.py
fromtypingimportAnyfromdrf_spectacular.utilsimportOpenApiResponsefromdrf_spectacular.utilsimportextend_schemafrompydanticimportValidationErrorfromrest_frameworkimportstatusfromrest_framework.requestimportRequestfromrest_framework.responseimportResponsefromrest_framework.viewsimportAPIViewfrom .models.serviceimportRequestSchemafrom .models.serviceimportResponseSchemafrom .models.serviceimportErrorResponseSchemafrom .services.serviceimportService# Instantiate the serviceservice=Service()
classView(APIView):
"Implements the DRF view to connect the Service to an API"@extend_schema(summary="Service",description="Services the submitted text",request=RequestSchema,responses={200: ResponseSchema,400: OpenApiResponse(ErrorResponseSchema,description="Invalid Request - message format is incorrect", ), }, )defpost(self, request: Request, *args: Any, **kwargs: Any) ->Response:
"""Serves the submitted text."""try:
# Validate the input using Pydanticvalidated_data=RequestSchema(**request.data)
result=service.service(validated_data)
exceptValidationErrorase:
result=ErrorResponseSchema(detail=str(e), code="VALIDATION_ERROR")
returnResponse(result.dict(), status=status.HTTP_400_BAD_REQUEST)
returnResponse(result.dict(), status=status.HTTP_200_OK)
Describe the bug
I write an app in django using the django rest framework and pydantic classes as well as spectacular extended_schema annotations.
when exporting views that take types having optional fields the export does not validate:
neither do classes of type
To Reproduce
models/service.py
services/service.py
views/service.py
urls.py:
python manage.py spectacular --file openapi-schema.yaml
exports as:
exports as:
both don't validate
Expected behavior
Exporting valid Pydantic types leads to valid OpenAPI specs.
python manage.py spectacular --file openapi-schema.yaml --validate
worksThe text was updated successfully, but these errors were encountered: