How to use the prance.util.formats.serialize_spec function in prance

To help you get started, we’ve selected a few prance examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jfinkhaeuser / prance / tests / test_util_formats.py View on Github external
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('{')
github jfinkhaeuser / prance / tests / test_util_formats.py View on Github external
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
github jfinkhaeuser / prance / tests / test_util_resolver.py View on Github external
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/specs/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
github jfinkhaeuser / prance / tests / test_util_resolver.py View on Github external
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/specs/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
github jfinkhaeuser / prance / tests / test_util_resolver.py View on Github external
def test_recursion_limit_set_limit_ignore(recursion_limit_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_file,
      fs.abspath('tests/specs/recursion_limit.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
github jfinkhaeuser / prance / tests / test_util_formats.py View on Github external
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
github jfinkhaeuser / prance / prance / cli.py View on Github external
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,))