Skip to content
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

yaml.dump of spec.to_dict() results in invalid spec #161

Closed
daenney opened this issue Oct 12, 2017 · 4 comments

Comments

@daenney
Copy link

commented Oct 12, 2017

I'm trying to dump the spec to YAML with yaml.dump(spec.to_dict()) but that results in an invalid representation due to entries like these:

  - - /api/v0/groups
    - !!python/object/new:apispec.core.Path

Or, for items:

items:
                  properties: !!python/object/new:apispec.lazy_dict.LazyDict
403:
              description: You are not authorized to request this resource
              schema:
                properties: !!python/object/new:apispec.lazy_dict.LazyDict
                  dictitems:
                    code: {format: int32, type: integer}
                    error: {type: string}
                required: [code, error]
                type: object

In order to dump this correctly it seems we'd need a custom YAML dumper for apispec. Use safe_dumper isn't possible either since it doesn't know how to dump certain of these objects.

@daenney

This comment has been minimized.

Copy link
Author

commented Oct 12, 2017

My current approach is yaml.dump(json.loads(json.dumps(spec.to_dict())), default_flow_style=False) which side-steps the issue.

@yoichi

This comment has been minimized.

Copy link
Collaborator

commented Oct 16, 2017

Add representer before yaml.dump() call as:

from collections import OrderedDict
from apispec.lazy_dict import LazyDict
from apispec.core import Path

def represent_dict(dumper, instance):
    return dumper.represent_mapping('tag:yaml.org,2002:map', instance.items())

yaml.add_representer(OrderedDict, represent_dict)
yaml.add_representer(LazyDict, represent_dict)
yaml.add_representer(Path, represent_dict)
@daenney

This comment has been minimized.

Copy link
Author

commented Oct 16, 2017

Awesome, thanks. That looks much better!

One other question, I run into a few cases where something like this shows up:

definitions:
  Group:
    properties:
      parent:
        allOf:
        - $ref: '#/definitions/Group'
        default: &id001 []

This comes from a definition like this:

class Group:
    [..]
    parent = fields.Nested('Group', exclude=('parent', 'children'), default=[])

This seems mostly fine and passes validation but it doesn't feel quite right to have &id001 show up in there. (fields is marshmallow.fields)

@yoichi

This comment has been minimized.

Copy link
Collaborator

commented Oct 20, 2017

@daenney I could not reproduce your situation. Could you share us a reproducable code (and the environment information)?

Regards,

jd added a commit to jd/apispec that referenced this issue Dec 18, 2017

jd added a commit to jd/apispec that referenced this issue Dec 18, 2017

jd added a commit to jd/apispec that referenced this issue Jan 8, 2018

jd added a commit to jd/apispec that referenced this issue Jan 10, 2018

@sloria sloria closed this in #177 Jan 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.