Verified Commit c307438b authored by Dan Jones's avatar Dan Jones
Browse files

refactor: make imported schemas available to tests

- revert changes to emergency schema
- remove Feature and FeatureCollection
parent dc538669
...@@ -13,64 +13,60 @@ ...@@ -13,64 +13,60 @@
"planning_config_ID": 1, "planning_config_ID": 1,
"exclusion_zones": [ "exclusion_zones": [
{ {
"geometry": { "type": "Polygon",
"type": "Polygon", "coordinates": [
"coordinates": [ [
[ [
[ -4.1777839187560915,
-4.1777839187560915, 50.34173405662855
50.34173405662855 ],
], [
[ -4.1777839187560915,
-4.1777839187560915, 50.33820949229701
50.33820949229701 ],
], [
[ -4.143667777943875,
-4.143667777943875, 50.33820949229701
50.33820949229701 ],
], [
[ -4.143667777943875,
-4.143667777943875, 50.34173405662855
50.34173405662855 ],
], [
[ -4.1777839187560915,
-4.1777839187560915, 50.34173405662855
50.34173405662855
]
] ]
] ]
} ]
} }
], ],
"region_of_interest": [ "region_of_interest": [
{ {
"geometry": { "type": "Polygon",
"type": "Polygon", "coordinates": [
"coordinates": [ [
[ [
[ -4.1777839187560915,
-4.1777839187560915, 50.34173405662855
50.34173405662855 ],
], [
[ -4.1777839187560915,
-4.1777839187560915, 50.33820949229701
50.33820949229701 ],
], [
[ -4.143667777943875,
-4.143667777943875, 50.33820949229701
50.33820949229701 ],
], [
[ -4.143667777943875,
-4.143667777943875, 50.34173405662855
50.34173405662855 ],
], [
[ -4.1777839187560915,
-4.1777839187560915, 50.34173405662855
50.34173405662855
]
] ]
] ]
} ]
} }
], ],
"squads": [ "squads": [
......
...@@ -34,7 +34,8 @@ emergency_schema = { ...@@ -34,7 +34,8 @@ emergency_schema = {
}, },
}, },
"required": [ "required": [
"target_waypoint", "target_waypoint_latitude",
"target_waypoint_longitude",
], ],
} }
......
...@@ -30,88 +30,92 @@ FLASK_PORT = os.getenv("FLASK_PORT", 5000) ...@@ -30,88 +30,92 @@ FLASK_PORT = os.getenv("FLASK_PORT", 5000)
# Switch on debug mode if env var is truthy # Switch on debug mode if env var is truthy
FLASK_DEBUG = os.getenv("FLASK_DEBUG", "False").lower() in ("true", "1", "t") FLASK_DEBUG = os.getenv("FLASK_DEBUG", "False").lower() in ("true", "1", "t")
swagger_config = {
"openapi": "3.0.2", def get_swagger_config():
"swagger_ui": True, swagger_config = {
"specs_route": "/", "openapi": "3.0.2",
"info": { "swagger_ui": True,
"title": "SoAR Backbone Message Formats", "specs_route": "/",
"version": "1.0", "info": {
"description": "SoAR message protocol in schemas", "title": "SoAR Backbone Message Formats",
}, "version": "1.0",
"specs": [ "description": "SoAR message protocol in schemas",
{ },
"endpoint": "swagger", "specs": [
"route": "/soar_protocol.json", {
} "endpoint": "swagger",
], "route": "/soar_protocol.json",
"url_prefix": URL_PREFIX, }
"paths": {}, ],
"components": { "url_prefix": URL_PREFIX,
"schemas": { "paths": {},
"MESSAGE": { "components": {
"type": "object", "schemas": {
"description": "Full message definition with" "MESSAGE": {
+ " message-metadata in `header` and different" "type": "object",
+ " message type schemas under `payload`", "description": "Full message definition with"
"properties": { + " message-metadata in `header` and different"
"header": { + " message type schemas under `payload`",
"$ref": "#/components/schemas/header", "properties": {
"header": {
"$ref": "#/components/schemas/header",
},
"payload": {"$ref": "#/components/schemas/payload"},
}, },
"payload": {"$ref": "#/components/schemas/payload"}, "required": ["header", "payload"],
}, },
"required": ["header", "payload"], "payload": {
}, "discriminator": {
"payload": { "propertyName": "message_type",
"discriminator": { "mapping": {
"propertyName": "message_type", "alert": "#/components/schemas/alert",
"mapping": { "mission_plan": "#/components/schemas/mission_plan",
"alert": "#/components/schemas/alert", "mission_plan_encoded": "#/components/schemas/"
"mission_plan": "#/components/schemas/mission_plan", + "mission_plan_encoded",
"mission_plan_encoded": "#/components/schemas/" "observation": "#/components/schemas/observation",
+ "mission_plan_encoded", "observation_encoded": "#/components/schemas/"
"observation": "#/components/schemas/observation", + "observation_encoded",
"observation_encoded": "#/components/schemas/" "planning_configuration": "#/components/schemas/"
+ "observation_encoded", + "planning_configuration",
"planning_configuration": "#/components/schemas/" "platform_status": "#/components/schemas/platform_status",
+ "planning_configuration", "platform_status_encoded": "#/components/schemas/"
"platform_status": "#/components/schemas/platform_status", + "platform_status_encoded",
"platform_status_encoded": "#/components/schemas/" "acknowledgement": "#/components/schemas/acknowledgement",
+ "platform_status_encoded", "survey": "#/components/schemas/survey",
"acknowledgement": "#/components/schemas/acknowledgement", "survey_encoded": "#/components/schemas/" + "survey_encoded",
"survey": "#/components/schemas/survey", },
"survey_encoded": "#/components/schemas/" + "survey_encoded",
}, },
"oneOf": [
{"$ref": "#/components/schemas/alert"},
{"$ref": "#/components/schemas/acknowledgement"},
{"$ref": "#/components/schemas/mission_plan"},
{"$ref": "#/components/schemas/mission_plan_encoded"},
{"$ref": "#/components/schemas/observation"},
{"$ref": "#/components/schemas/observation_encoded"},
{"$ref": "#/components/schemas/planning_configuration"},
{"$ref": "#/components/schemas/platform_status"},
{"$ref": "#/components/schemas/platform_status_encoded"},
{"$ref": "#/components/schemas/survey"},
{"$ref": "#/components/schemas/survey_encoded"},
],
}, },
"oneOf": [ "header": message_header,
{"$ref": "#/components/schemas/alert"}, "mission_plan": mission_plan_schema,
{"$ref": "#/components/schemas/acknowledgement"}, "mission_plan_encoded": mission_plan_encoded_schema,
{"$ref": "#/components/schemas/mission_plan"}, "observation": observation_schema,
{"$ref": "#/components/schemas/mission_plan_encoded"}, "observation_encoded": observation_encoded_schema,
{"$ref": "#/components/schemas/observation"}, "planning_configuration": planning_configuration_schema,
{"$ref": "#/components/schemas/observation_encoded"}, "platform_status": platform_status_schema,
{"$ref": "#/components/schemas/planning_configuration"}, "platform_status_encoded": platform_status_encoded_schema,
{"$ref": "#/components/schemas/platform_status"}, "survey": survey_schema,
{"$ref": "#/components/schemas/platform_status_encoded"}, "survey_encoded": survey_encoded_schema,
{"$ref": "#/components/schemas/survey"}, "acknowledgement": acknowledgement_schema,
{"$ref": "#/components/schemas/survey_encoded"}, "alert": alert_schema,
], }
}, },
"header": message_header, }
"mission_plan": mission_plan_schema, import_remote_refs(swagger_config)
"mission_plan_encoded": mission_plan_encoded_schema, return swagger_config
"observation": observation_schema,
"observation_encoded": observation_encoded_schema,
"planning_configuration": planning_configuration_schema,
"platform_status": platform_status_schema,
"platform_status_encoded": platform_status_encoded_schema,
"survey": survey_schema,
"survey_encoded": survey_encoded_schema,
"acknowledgement": acknowledgement_schema,
"alert": alert_schema,
}
},
}
def resolve_ref(ref): def resolve_ref(ref):
...@@ -172,6 +176,8 @@ def inject_schema(schema, remote_ref): ...@@ -172,6 +176,8 @@ def inject_schema(schema, remote_ref):
local_name = rename_ref(remote_ref) local_name = rename_ref(remote_ref)
local_ref = f"#/components/schemas/{local_name}" local_ref = f"#/components/schemas/{local_name}"
ref_schema = resolve_ref(remote_ref) ref_schema = resolve_ref(remote_ref)
del ref_schema["$id"]
del ref_schema["$schema"]
if ref_schema is not None: if ref_schema is not None:
nested_replace(schema, "$ref", remote_ref, local_ref) nested_replace(schema, "$ref", remote_ref, local_ref)
schema["components"]["schemas"][local_name] = ref_schema schema["components"]["schemas"][local_name] = ref_schema
...@@ -180,16 +186,19 @@ def inject_schema(schema, remote_ref): ...@@ -180,16 +186,19 @@ def inject_schema(schema, remote_ref):
return False return False
def import_remote_refs(): def import_remote_refs(swagger_config):
""" """
inject the following remote refs into the schema inject the following remote refs into the schema
and replace the remote refs with local refs and replace the remote refs with local refs
returns True if all schemas resolved and injected returns True if all schemas resolved and injected
""" """
# For some reason importing Feature or FeatureCollection
# makes the schema fail to validate
ref_imports = [ ref_imports = [
"https://geojson.org/schema/Feature.json", # "https://geojson.org/schema/Feature.json",
"https://geojson.org/schema/FeatureCollection.json", # "https://geojson.org/schema/FeatureCollection.json",
"https://geojson.org/schema/LineString.json", "https://geojson.org/schema/LineString.json",
"https://geojson.org/schema/MultiLineString.json", "https://geojson.org/schema/MultiLineString.json",
"https://geojson.org/schema/MultiPoint.json", "https://geojson.org/schema/MultiPoint.json",
...@@ -312,7 +321,7 @@ def get_options(): ...@@ -312,7 +321,7 @@ def get_options():
if __name__ == "__main__": if __name__ == "__main__":
import_remote_refs() swagger_config = get_swagger_config()
# Parse script args # Parse script args
config = get_options() config = get_options()
......
This diff is collapsed.
...@@ -5,7 +5,7 @@ from jsonschema.validators import RefResolver ...@@ -5,7 +5,7 @@ from jsonschema.validators import RefResolver
import unittest import unittest
import json import json
import os import os
from generate_schema_config import write_schema, swagger_config from generate_schema_config import write_schema, get_swagger_config
MOCK_DATA_DIR = "examples/" MOCK_DATA_DIR = "examples/"
...@@ -24,6 +24,7 @@ class SchemaTestCase(unittest.TestCase): ...@@ -24,6 +24,7 @@ class SchemaTestCase(unittest.TestCase):
def setUpClass(cls): def setUpClass(cls):
test_schema_path = "tests/test_swagger.json" test_schema_path = "tests/test_swagger.json"
os.environ["SCHEMA_PATH"] = test_schema_path os.environ["SCHEMA_PATH"] = test_schema_path
swagger_config = get_swagger_config()
write_schema(swagger_config, test_schema_path) write_schema(swagger_config, test_schema_path)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment