| 1 |  |  | """Custom validator for the Annif API.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from __future__ import annotations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from typing import Any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from connexion.exceptions import BadRequestProblem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from connexion.json_schema import format_error_with_path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from connexion.validators import JSONRequestBodyValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from jsonschema.exceptions import ValidationError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | logger = logging.getLogger("openapi.validation") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class CustomRequestBodyValidator(JSONRequestBodyValidator): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     """Custom request body validator that overrides the default error message for the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     'maxItems' validator for the 'documents' property.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     def __init__(self, *args, **kwargs) -> None: | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |         super().__init__(*args, **kwargs) | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |     def _validate(self, body: Any) -> dict | None: | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         if not self._nullable and body is None: | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |             raise BadRequestProblem( | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |                 "Request body must not be empty" | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |             )  # pragma: no cover | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         try: | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |             return self._validator.validate(body) | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |         except ValidationError as exception: | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |             # Prevent logging request body with contents of all documents | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |             if exception.validator == "maxItems" and list(exception.schema_path) == [ | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |                 "properties", | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |                 "documents", | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |                 "maxItems", | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |             ]: | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |                 exception.message = "too many items" | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |             error_path_msg = format_error_with_path(exception=exception) | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |             logger.error( | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |                 f"Validation error: {exception.message}{error_path_msg}", | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |                 extra={"validator": "body"}, | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |             ) | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |             raise BadRequestProblem(detail=f"{exception.message}{error_path_msg}") | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |  |