Skip to content

Missing Info models #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
15 changes: 15 additions & 0 deletions openapi_core/schema/contacts/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""OpenAPI core contacts factories module"""
from openapi_core.schema.contacts.models import Contact


class ContactFactory(object):

def __init__(self, dereferencer):
self.dereferencer = dereferencer

def create(self, contact_spec):
contact_deref = self.dereferencer.dereference(contact_spec)
name = contact_deref.get('name')
url = contact_deref.get('url')
email = contact_deref.get('email')
return Contact(name=name, url=url, email=email)
9 changes: 9 additions & 0 deletions openapi_core/schema/contacts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""OpenAPI core contacts models module"""


class Contact(object):

def __init__(self, name=None, url=None, email=None):
self.name = name
self.url = url
self.email = email
32 changes: 31 additions & 1 deletion openapi_core/schema/infos/factories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""OpenAPI core infos factories module"""
from openapi_core.compat import lru_cache
from openapi_core.schema.contacts.factories import ContactFactory
from openapi_core.schema.infos.models import Info
from openapi_core.schema.licenses.factories import LicenseFactory


class InfoFactory(object):
Expand All @@ -11,4 +14,31 @@ def create(self, info_spec):
info_deref = self.dereferencer.dereference(info_spec)
title = info_deref['title']
version = info_deref['version']
return Info(title, version)
description = info_deref.get('description')
terms_of_service = info_deref.get('termsOfService')

contact = None
if 'contact' in info_deref:
contact_spec = info_deref.get('contact')
contact = self.contact_factory.create(contact_spec)

license = None
if 'license' in info_deref:
license_spec = info_deref.get('license')
license = self.license_factory.create(license_spec)

return Info(
title, version,
description=description, terms_of_service=terms_of_service,
contact=contact, license=license,
)

@property
@lru_cache()
def contact_factory(self):
return ContactFactory(self.dereferencer)

@property
@lru_cache()
def license_factory(self):
return LicenseFactory(self.dereferencer)
9 changes: 8 additions & 1 deletion openapi_core/schema/infos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

class Info(object):

def __init__(self, title, version):
def __init__(
self, title, version, description=None, terms_of_service=None,
contact=None, license=None,
):
self.title = title
self.version = version
self.description = description
self.terms_of_service = terms_of_service
self.contact = contact
self.license = license
Empty file.
14 changes: 14 additions & 0 deletions openapi_core/schema/licenses/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""OpenAPI core licenses factories module"""
from openapi_core.schema.licenses.models import License


class LicenseFactory(object):

def __init__(self, dereferencer):
self.dereferencer = dereferencer

def create(self, license_spec):
license_deref = self.dereferencer.dereference(license_spec)
name = license_deref['name']
url = license_deref.get('url')
return License(name, url=url)
8 changes: 8 additions & 0 deletions openapi_core/schema/licenses/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""OpenAPI core licenses models module"""


class License(object):

def __init__(self, name, url=None):
self.name = name
self.url = url
7 changes: 7 additions & 0 deletions tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: Swagger Petstore API specification
termsOfService: Fair use
contact:
name: Author
url: http://petstore.swagger.io
email: [email protected]
license:
name: MIT
url: https://opensource.org/licenses/MIT
servers:
- url: http://petstore.swagger.io/{version}
variables:
Expand Down
17 changes: 15 additions & 2 deletions tests/integration/schema/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,21 @@ def response_validator(self, spec):

def test_spec(self, spec, spec_dict):
url = 'http://petstore.swagger.io/v1'
assert spec.info.title == spec_dict['info']['title']
assert spec.info.version == spec_dict['info']['version']

info_spec = spec_dict['info']
assert spec.info.title == info_spec['title']
assert spec.info.description == info_spec['description']
assert spec.info.terms_of_service == info_spec['termsOfService']
assert spec.info.version == info_spec['version']

contact_spec = info_spec['contact']
assert spec.info.contact.name == contact_spec['name']
assert spec.info.contact.url == contact_spec['url']
assert spec.info.contact.email == contact_spec['email']

license_spec = info_spec['license']
assert spec.info.license.name == license_spec['name']
assert spec.info.license.url == license_spec['url']

assert spec.get_server_url() == url

Expand Down