def test_parse_yaml(): yaml = """--- foo: bar """ parsed = formats.parse_spec(yaml, 'foo.yaml') assert parsed['foo'] == 'bar', 'Did not parse with explicit YAML' parsed = formats.parse_spec(yaml) assert parsed['foo'] == 'bar', 'Did not parse with implicit YAML'
def test_parse_yaml(): yaml = """--- foo: bar """ parsed = formats.parse_spec(yaml, "foo.yaml") assert parsed["foo"] == "bar", "Did not parse with explicit YAML" parsed = formats.parse_spec(yaml) assert parsed["foo"] == "bar", "Did not parse with implicit YAML"
def test_issue_65_partial_resolution_files(): specs = '''openapi: "3.0.0" info: title: '' version: '1.0.0' paths: {} components: schemas: SampleArray: type: array items: $ref: '#/components/schemas/ItemType' ItemType: type: integer ''' from prance.util import formats specs = formats.parse_spec(specs, 'issue_65.yaml') res = resolver.RefResolver(specs, fs.abspath('issue_65.yaml'), resolve_types = resolver.RESOLVE_FILES ) res.resolve_references() from prance.util.path import path_get val = path_get(res.specs, ('components', 'schemas', 'SampleArray', 'items')) assert '$ref' in val
def test_issue_77_translate_external_refs_internal(): specs = '' with open('tests/specs/issue_78/openapi.json', 'r') as fh: specs = fh.read() from prance.util import formats specs = formats.parse_spec(specs, 'openapi.json') res = resolver.RefResolver(specs, fs.abspath('openapi.json'), resolve_types = resolver.RESOLVE_FILES | resolver.RESOLVE_INTERNAL, resolve_method= resolver.TRANSLATE_EXTERNAL ) res.resolve_references() from prance.util.path import path_get val = path_get(res.specs, ('components', 'schemas', '_schemas.json_Body')) # Reference to file is translated in components/schemas assert 'content' in val assert 'application/json' in val['content'] # Internal Reference links updated assert '#/components/schemas/_schemas.json_Something' == val['content']['application/json']['$ref'] # Internal references is copied to componnents/schemas seperately val = path_get(res.specs, ('components', 'schemas', '_schemas.json_Something')) assert 'type' in val # File reference url is updated as well val = path_get(res.specs, ('paths', '/endpoint', 'post', 'requestBody', '$ref')) assert val == '#/components/schemas/_schemas.json_Body'
def get_specs(fname): specs = fs.read_file(fname) from prance.util import formats specs = formats.parse_spec(specs, fname) return specs
def test_convert_petstore_json(petstore_json): converted, content_type = convert.convert_str(petstore_json) # Check correct content type assert 'json' in content_type # Parsing can't fail. from prance.util import formats parsed = formats.parse_spec(converted, content_type=content_type) # Assert the correct target version assert 'openapi' in parsed assert parsed['openapi'].startswith('3.')
def test_convert_url(): from prance.util import url converted, content_type = convert.convert_url(url.absurl('python://tests/specs/petstore.yaml')) # Check correct content type assert 'yaml' in content_type # Parsing can't fail. from prance.util import formats parsed = formats.parse_spec(converted, content_type = content_type) # Assert the correct target version assert 'openapi' in parsed assert parsed['openapi'].startswith('3.')
def test_convert_petstore_yaml(petstore_yaml): converted, content_type = convert.convert_str(petstore_yaml) # Check correct content type assert "yaml" in content_type # Parsing can't fail. from prance.util import formats parsed = formats.parse_spec(converted, content_type=content_type) # Assert the correct target version assert "openapi" in parsed assert parsed["openapi"].startswith("3.")
def test_issue_205_swagger_resolution_failure(): specs = '' with open('tests/specs/kubernetes_api_docs.json', 'r') as fh: specs = fh.read() from prance.util import formats specs = formats.parse_spec(specs, 'kubernetes_api_docs.json') res = resolver.RefResolver(specs, fs.abspath('kubernetes_api_docs.json'), resolve_types = resolver.RESOLVE_FILES, resolve_method= resolver.TRANSLATE_EXTERNAL ) # test will raise an exception res.resolve_references()
def test_issue_78_resolve_internal_bug(): specs = '' with open('tests/specs/issue_78/openapi.json', 'r') as fh: specs = fh.read() from prance.util import formats specs = formats.parse_spec(specs, 'openapi.json') res = resolver.RefResolver(specs, fs.abspath('openapi.json'), resolve_types = resolver.RESOLVE_FILES ) res.resolve_references() from prance.util.path import path_get val = path_get(res.specs, ('paths', '/endpoint', 'post', 'requestBody', 'content')) # Reference to file is resolved assert 'application/json' in val # Internal reference within file is NOT resolved assert '$ref' in val['application/json']
def test_issue_77_internal_refs_unresolved(): specs = '' with open('tests/specs/issue_78/openapi.json', 'r') as fh: specs = fh.read() from prance.util import formats specs = formats.parse_spec(specs, 'openapi.json') res = resolver.RefResolver(specs, fs.abspath('openapi.json'), resolve_types=resolver.RESOLVE_FILES, resolve_method=resolver.TRANSLATE_EXTERNAL) res.resolve_references() from prance.util.path import path_get val = path_get(res.specs, ('components', 'schemas')) # File reference resolved assert '_schemas.json_Body' in val # Internal file reference not resolved assert '_schemas.json_Something' not in val
def test_parse_json(): json = '{ "foo": "bar" }' parsed = formats.parse_spec(json, "foo.js") assert parsed["foo"] == "bar", "Did not parse with explicit JSON"
def test_parse_json(): json = '{ "foo": "bar" }' parsed = formats.parse_spec(json, 'foo.js') assert parsed['foo'] == 'bar', 'Did not parse with explicit JSON'
def test_parse_yaml_ctype(): yaml = """--- foo: bar """ parsed = formats.parse_spec(yaml, None, content_type = 'text/yaml') assert parsed['foo'] == 'bar', 'Did not parse with explicit YAML'
def test_parse_unknown_ext(): with pytest.raises(formats.ParseError): formats.parse_spec('{-', 'asdf.xml')
def test_parse_json_ctype(): json = '{ "foo": "bar" }' parsed = formats.parse_spec(json, None, content_type = 'application/json') assert parsed['foo'] == 'bar', 'Did not parse with explicit JSON'
def test_parse_yaml_ctype(): yaml = """--- foo: bar """ parsed = formats.parse_spec(yaml, None, content_type="text/yaml") assert parsed["foo"] == "bar", "Did not parse with explicit YAML"
def test_parse_unknown_ctype(): with pytest.raises(formats.ParseError): formats.parse_spec("{-", None, content_type="text/xml")
def test_parse_unknown_ext(): with pytest.raises(formats.ParseError): formats.parse_spec("{-", "asdf.xml")
def test_parse_unknown(): with pytest.raises(formats.ParseError): formats.parse_spec("{-")
def test_parse_json_ctype(): json = '{ "foo": "bar" }' parsed = formats.parse_spec(json, None, content_type="application/json") assert parsed["foo"] == "bar", "Did not parse with explicit JSON"