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

Generate a JSON schema for me #145

Open
bowlofeggs opened this issue Mar 19, 2019 · 3 comments
Open

Generate a JSON schema for me #145

bowlofeggs opened this issue Mar 19, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@bowlofeggs
Copy link
Contributor

Greetings!

I've been working on writing JSON schemas for Bodhi's messages the past few days. It's pretty rad, but I had the thought that it might be a nice convenience if fedora-messaging had the ability to generate these for me based on Python classes. I don't know exactly how I would recommend making it work, but I think it could be cool if we require Python 3 for this feature so we can use some cool Python 3 things to help out:

  1. Type annotations! This way we can know the types of the items to fill in the schema in a "Pythonic" way.
  2. We could use typing.NamedTuple - this is like a really fancy Enum that supports storing data with type annotations and behaviors.

Anyways, I'm quite fuzzy on the details, but I imagine some code that can introspect a class and spit out a JSON schema, and all fedora-messaging API users have to do is write nice Python classes and not worry about how JSON schema works.

Note: We would need some way to differentiate between required fields and non-required fields to fill out the required section on objects in the JSON schema. Perhaps just a _required = ['a', 'b', 'c'] on the class could do that for us?

@bowlofeggs bowlofeggs added the enhancement New feature or request label Mar 19, 2019
@bowlofeggs
Copy link
Contributor Author

To solve the problem of Python 3 being used, it may be possible to have the requested feature live in its own module and have the Python 2 version avoid packaging it.

@bowlofeggs
Copy link
Contributor Author

bowlofeggs commented Mar 19, 2019

import typing


class CoolThingIWant(typing.NamedTuple):
    <secret code goes here>


class User(CoolThingIWant):
    name: str
    email: str
    _required: ['name', 'email']


class Comment(CoolThingIWant):
    text: str
    user: User
    _required: ['text', 'user']
    _schema_description: str = 'A comment'


<<< print Comment._json_schema
{
        'id': f'<generate_this_somehow_maybe_using_class_name_or_some_more_metadata#',
        '$schema': 'http://json-schema.org/draft-04/schema#',
        'description': 'A comment',
        'type': 'object',
        'properties': {
            'text': {
                'type': 'string'
                'description': 'Not sure how we would generate this. Need to find a way.'
            },
            'user': {
                'type': 'object',
                <blah blah blah>
            }
        }
        <blah blah blah>
}

@bowlofeggs
Copy link
Contributor Author

Or maybe I could do this in the https://python-jsonschema.readthedocs.io/en/latest/ project? Not sure if I would want to be able to mix with fedora-messaging features or not. I guess there are always mixin classes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant