Coverage for src/configuration/openapi_schema_validate.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-03-24 22:02 -0400

1"""Validator for OpenAPI schema 

2""" 

3 

4import os 

5from uuid import uuid4 

6 

7from openapi_spec_validator import validate 

8from openapi_spec_validator.readers import read_from_filename 

9 

10from common.models.openapi_spec import OpenAPISpec 

11 

12 

13def validate_openapi_spec(spec: OpenAPISpec, temp_file_dir="/tmp"): 

14 """Raises an error if the OpenAPI spec text is not a valid OpenAPI spec.""" 

15 # Build file at /tmp from text in spec.spec_text 

16 # This is advantageous because we keep the text in the DB 

17 # and avoid having to have a distributed file system - we only 

18 # need this temp file on the host (in the container) that our program 

19 # is running on (in). 

20 file_name = "cuecode_openapi_spec_" + str(uuid4()) 

21 file_path = os.path.join(temp_file_dir, file_name) 

22 with open(file_path, "w", encoding="UTF-8") as f: 

23 f.write(spec.spec_text) 

24 spec_tuple = read_from_filename(file_path) 

25 validate(spec_tuple[0])