From c4d4ed8515be86138d1bb8a86ff101852b2e4117 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Tue, 22 Oct 2019 18:21:40 +0100 Subject: [PATCH] Server missing fields --- openapi_core/schema/operations/generators.py | 10 ++++++++++ openapi_core/schema/servers/generators.py | 6 ++---- openapi_core/schema/servers/models.py | 3 ++- openapi_core/schema/specs/factories.py | 5 +++++ tests/integration/data/v3.0/petstore.yaml | 5 +++++ tests/integration/schema/test_spec.py | 17 +++++++++++++++++ 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/openapi_core/schema/operations/generators.py b/openapi_core/schema/operations/generators.py index 7608137..63fed4a 100644 --- a/openapi_core/schema/operations/generators.py +++ b/openapi_core/schema/operations/generators.py @@ -12,6 +12,7 @@ from openapi_core.schema.parameters.generators import ParametersGenerator from openapi_core.schema.request_bodies.factories import RequestBodyFactory from openapi_core.schema.responses.generators import ResponsesGenerator from openapi_core.schema.security.factories import SecurityRequirementFactory +from openapi_core.schema.servers.generators import ServersGenerator class OperationsGenerator(object): @@ -39,6 +40,9 @@ class OperationsGenerator(object): summary = operation_deref.get('summary') description = operation_deref.get('description') security_requirements_list = operation_deref.get('security', []) + servers_spec = operation_deref.get('servers', []) + + servers = self.servers_generator.generate(servers_spec) security = None if security_requirements_list: @@ -66,6 +70,7 @@ class OperationsGenerator(object): external_docs=external_docs, security=security, request_body=request_body, deprecated=deprecated, operation_id=operation_id, tags=list(tags_list), + servers=servers, ), ) @@ -93,3 +98,8 @@ class OperationsGenerator(object): @lru_cache() def security_requirement_factory(self): return SecurityRequirementFactory(self.dereferencer) + + @property + @lru_cache() + def servers_generator(self): + return ServersGenerator(self.dereferencer) diff --git a/openapi_core/schema/servers/generators.py b/openapi_core/schema/servers/generators.py index d05b589..c9c41b6 100644 --- a/openapi_core/schema/servers/generators.py +++ b/openapi_core/schema/servers/generators.py @@ -12,18 +12,16 @@ class ServersGenerator(object): def generate(self, servers_spec): servers_deref = self.dereferencer.dereference(servers_spec) - if not servers_deref: - yield Server('/') - return for server_spec in servers_deref: url = server_spec['url'] variables_spec = server_spec.get('variables', {}) + description = server_spec.get('description') variables = None if variables_spec: variables = self.variables_generator.generate(variables_spec) - yield Server(url, variables=variables) + yield Server(url, variables=variables, description=description) @property @lru_cache() diff --git a/openapi_core/schema/servers/models.py b/openapi_core/schema/servers/models.py index f5754c4..08e72ab 100644 --- a/openapi_core/schema/servers/models.py +++ b/openapi_core/schema/servers/models.py @@ -4,9 +4,10 @@ from six import iteritems class Server(object): - def __init__(self, url, variables=None): + def __init__(self, url, variables=None, description=None): self.url = url self.variables = variables and dict(variables) or {} + self.description = description @property def default_url(self): diff --git a/openapi_core/schema/specs/factories.py b/openapi_core/schema/specs/factories.py index 0d31dd6..ad73425 100644 --- a/openapi_core/schema/specs/factories.py +++ b/openapi_core/schema/specs/factories.py @@ -30,6 +30,11 @@ class SpecFactory(object): paths = spec_dict_deref.get('paths', {}) components_spec = spec_dict_deref.get('components', {}) + if not servers_spec: + servers_spec = [ + {'url': '/'}, + ] + info = self.info_factory.create(info_spec) servers = self.servers_generator.generate(servers_spec) paths = self.paths_generator.generate(paths) diff --git a/tests/integration/data/v3.0/petstore.yaml b/tests/integration/data/v3.0/petstore.yaml index 8976d67..241309a 100644 --- a/tests/integration/data/v3.0/petstore.yaml +++ b/tests/integration/data/v3.0/petstore.yaml @@ -70,6 +70,11 @@ paths: - petstore_auth: - write:pets - read:pets + servers: + - url: https://development.gigantic-server.com/v1 + description: Development server + - url: https://staging.gigantic-server.com/v1 + description: Staging server operationId: createPets tags: - pets diff --git a/tests/integration/schema/test_spec.py b/tests/integration/schema/test_spec.py index 39a42d5..f28873c 100644 --- a/tests/integration/schema/test_spec.py +++ b/tests/integration/schema/test_spec.py @@ -100,6 +100,23 @@ class TestPetstore(object): assert sec_req.name == sec_req_nam assert sec_req.scope_names == sec_req_spec[sec_req_nam] + servers_spec = operation_spec.get('servers', []) + for idx, server in enumerate(operation.servers): + assert type(server) == Server + + server_spec = servers_spec[idx] + assert server.url == server_spec['url'] + assert server.default_url == server_spec['url'] + assert server.description == server_spec.get('description') + + for variable_name, variable in iteritems(server.variables): + assert type(variable) == ServerVariable + assert variable.name == variable_name + + variable_spec = server_spec['variables'][variable_name] + assert variable.default == variable_spec['default'] + assert variable.enum == variable_spec.get('enum') + responses_spec = operation_spec.get('responses') for http_status, response in iteritems(operation.responses):