Server missing fields

This commit is contained in:
p1c2u 2019-10-22 18:21:40 +01:00
parent d7840d0d61
commit c4d4ed8515
6 changed files with 41 additions and 5 deletions

View file

@ -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.request_bodies.factories import RequestBodyFactory
from openapi_core.schema.responses.generators import ResponsesGenerator from openapi_core.schema.responses.generators import ResponsesGenerator
from openapi_core.schema.security.factories import SecurityRequirementFactory from openapi_core.schema.security.factories import SecurityRequirementFactory
from openapi_core.schema.servers.generators import ServersGenerator
class OperationsGenerator(object): class OperationsGenerator(object):
@ -39,6 +40,9 @@ class OperationsGenerator(object):
summary = operation_deref.get('summary') summary = operation_deref.get('summary')
description = operation_deref.get('description') description = operation_deref.get('description')
security_requirements_list = operation_deref.get('security', []) security_requirements_list = operation_deref.get('security', [])
servers_spec = operation_deref.get('servers', [])
servers = self.servers_generator.generate(servers_spec)
security = None security = None
if security_requirements_list: if security_requirements_list:
@ -66,6 +70,7 @@ class OperationsGenerator(object):
external_docs=external_docs, security=security, external_docs=external_docs, security=security,
request_body=request_body, deprecated=deprecated, request_body=request_body, deprecated=deprecated,
operation_id=operation_id, tags=list(tags_list), operation_id=operation_id, tags=list(tags_list),
servers=servers,
), ),
) )
@ -93,3 +98,8 @@ class OperationsGenerator(object):
@lru_cache() @lru_cache()
def security_requirement_factory(self): def security_requirement_factory(self):
return SecurityRequirementFactory(self.dereferencer) return SecurityRequirementFactory(self.dereferencer)
@property
@lru_cache()
def servers_generator(self):
return ServersGenerator(self.dereferencer)

View file

@ -12,18 +12,16 @@ class ServersGenerator(object):
def generate(self, servers_spec): def generate(self, servers_spec):
servers_deref = self.dereferencer.dereference(servers_spec) servers_deref = self.dereferencer.dereference(servers_spec)
if not servers_deref:
yield Server('/')
return
for server_spec in servers_deref: for server_spec in servers_deref:
url = server_spec['url'] url = server_spec['url']
variables_spec = server_spec.get('variables', {}) variables_spec = server_spec.get('variables', {})
description = server_spec.get('description')
variables = None variables = None
if variables_spec: if variables_spec:
variables = self.variables_generator.generate(variables_spec) variables = self.variables_generator.generate(variables_spec)
yield Server(url, variables=variables) yield Server(url, variables=variables, description=description)
@property @property
@lru_cache() @lru_cache()

View file

@ -4,9 +4,10 @@ from six import iteritems
class Server(object): class Server(object):
def __init__(self, url, variables=None): def __init__(self, url, variables=None, description=None):
self.url = url self.url = url
self.variables = variables and dict(variables) or {} self.variables = variables and dict(variables) or {}
self.description = description
@property @property
def default_url(self): def default_url(self):

View file

@ -30,6 +30,11 @@ class SpecFactory(object):
paths = spec_dict_deref.get('paths', {}) paths = spec_dict_deref.get('paths', {})
components_spec = spec_dict_deref.get('components', {}) components_spec = spec_dict_deref.get('components', {})
if not servers_spec:
servers_spec = [
{'url': '/'},
]
info = self.info_factory.create(info_spec) info = self.info_factory.create(info_spec)
servers = self.servers_generator.generate(servers_spec) servers = self.servers_generator.generate(servers_spec)
paths = self.paths_generator.generate(paths) paths = self.paths_generator.generate(paths)

View file

@ -70,6 +70,11 @@ paths:
- petstore_auth: - petstore_auth:
- write:pets - write:pets
- read: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 operationId: createPets
tags: tags:
- pets - pets

View file

@ -100,6 +100,23 @@ class TestPetstore(object):
assert sec_req.name == sec_req_nam assert sec_req.name == sec_req_nam
assert sec_req.scope_names == sec_req_spec[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') responses_spec = operation_spec.get('responses')
for http_status, response in iteritems(operation.responses): for http_status, response in iteritems(operation.responses):