def test_serialize_json(): specs = { 'foo': 'bar', 'baz': [1, 2, 3], } # With no further information given, the specs must be in JSON serialized = formats.serialize_spec(specs) assert serialized.startswith('{') # The same must be the case if we provide a JSON file name serialized = formats.serialize_spec(specs, 'foo.json') assert serialized.startswith('{')
def test_serialize_json(): specs = { "foo": "bar", "baz": [1, 2, 3], } # With no further information given, the specs must be in JSON serialized = formats.serialize_spec(specs) assert serialized.startswith("{") # The same must be the case if we provide a JSON file name serialized = formats.serialize_spec(specs, "foo.json") assert serialized.startswith("{")
def test_recursion_limit_set_limit_ignore_files(recursion_limit_files_file): # If we overload the handler, and set the recursion limit higher, # we should get nested Pet objects a few levels deep. import os.path res = resolver.RefResolver(recursion_limit_files_file, fs.abspath('tests/recursion_limit_files.yaml'), recursion_limit = 2, recursion_limit_handler = recursion_limit_handler_none) res.resolve_references() from prance.util import formats contents = formats.serialize_spec(res.specs, 'foo.yaml') # The effect of returning None on recursion limit should be that # despite having recursion, the outermost reference to # definitions/Pet should get resolved. assert 'properties' in res.specs['paths']['/pets']['get']['responses']['200']['schema'] # However, the 'next' field should be resolved due to the higher recursion limit next_field = res.specs['paths']['/pets']['get']['responses']['200']['schema']['properties']['next']['schema'] assert next_field is not None # But the 'next' field of the 'next' field should not be resolved. assert next_field['properties']['next']['schema'] is None
def compile(ctx, url_or_path, output_file): """ Compile the given spec, resolving references if required. Resolves references and uses backends exactly as in the "validate" command, but only works on single URLs. If an output file name is given, output is written there, otherwise it is written to the terminal. """ # Create parser to use parser, name = __parser_for_url(url_or_path, ctx.obj['resolve'], ctx.obj['backend'], ctx.obj['strict'], ctx.obj['encoding']) # Try parsing __validate(parser, name) # Write output from prance.util import formats contents = formats.serialize_spec(parser.specification, output_file) if output_file is None: click.echo(contents) else: from .util import fs fs.write_file(output_file, contents) click.echo('Output written to "%s".' % (output_file,))
def __write_to_file(filename, specs): # noqa: N802 """ Write specs to the given filename. This takes into account file name extensions as per `fs.write_file`. """ from prance.util import fs, formats contents = formats.serialize_spec(specs, filename) fs.write_file(filename, contents)
def test_serialize_yaml_ctype(): specs = { "foo": "bar", "baz": [1, 2, 3], } # Force YAML with content type serialized = formats.serialize_spec(specs, None, content_type="text/yaml") assert "foo: bar" in serialized
def test_serialize_yaml(): specs = { "foo": "bar", "baz": [1, 2, 3], } # Provide a YAML file name serialized = formats.serialize_spec(specs, "foo.yml") assert "foo: bar" in serialized
def test_serialize_yaml(): specs = { 'foo': 'bar', 'baz': [1, 2, 3], } # Provide a YAML file name serialized = formats.serialize_spec(specs, 'foo.yml') assert 'foo: bar' in serialized
def test_serialize_yaml_ctype(): specs = { 'foo': 'bar', 'baz': [1, 2, 3], } # Force YAML with content type serialized = formats.serialize_spec(specs, None, content_type = 'text/yaml') assert 'foo: bar' in serialized
def test_serialize_json_ctype(): specs = { 'foo': 'bar', 'baz': [1, 2, 3], } # Force JSON with content type serialized = formats.serialize_spec(specs, None, content_type = 'application/json') assert serialized.startswith('{')
def test_serialize_json_ctype(): specs = { "foo": "bar", "baz": [1, 2, 3], } # Force JSON with content type serialized = formats.serialize_spec(specs, None, content_type="application/json") assert serialized.startswith("{")
def merge_swagger_file(main_file: str): """ Generate swagger into a single file :param main_file: Swagger file path :return: """ input_file = Path(main_file) output_file = Path(input_file.parent, 'swagger-complete.yaml') contents = formats.serialize_spec( specs=get_bundled_specs(input_file), filename=output_file, ) fs.write_file(filename=output_file, contents=contents, encoding='utf-8')
def test_recursion_limit_do_not_recurse_ignore_files(recursion_limit_files_file): # If we overload the handler, we should not get an error but should # also simply not have the 'next' field - or it should be None import os.path res = resolver.RefResolver(recursion_limit_files_file, fs.abspath('tests/recursion_limit_files.yaml'), recursion_limit_handler = recursion_limit_handler_none) res.resolve_references() from prance.util import formats contents = formats.serialize_spec(res.specs, 'foo.yaml') # The effect of returning None on recursion limit should be that # despite having recursion, the outermost reference to # definitions/Pet should get resolved. assert 'properties' in res.specs['paths']['/pets']['get']['responses']['200']['schema'] # However, the 'next' field should not be resolved. assert res.specs['paths']['/pets']['get']['responses']['200']['schema']['properties']['next']['schema'] is None