Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Local Definitions #185

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

elael
Copy link

@elael elael commented Jun 5, 2022

Greetings!

I'm proposing a new feature in this PR.

The Issue

We don't have control over the class whenever using an external library and thus need to use FieldEncoders.
The issue arises when writing the FieldEncoder for a complex external object. Precisely when writing the JSON schema.

For example, when loading a protobuf object, one can parse using the ParseDict helper function. When it comes to defining the JSON schema, one would want to create something similar to this structure:

{
            "type": "object",
            "properties": {
                "ex_foo": {"$ref": "#/definitions/EX_FOO"},
                "ex_bar": {"$ref": "#/definitions/EX_BAR"},
            },
            "$schema": "http://json-schema.org/draft-06/schema#",
            "definitions": {
                "EX_FOO": {
                    "type": "object",
                    "properties": {
                        "ex_sub_foo": {
                            "type": "array",
                            "items": {"$ref": "#/definitions/EX_SUB_FOO"},
                        }
                    },
                },
                "EX_BAR": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string"},
                    },
                },
                "EX_SUB_FOO": {
                    "type": "object",
                    "properties": {"number": {"type": "integer"}},
                },
            },
        }

Where the definitions live in "definitions", but returning that in the FieldEncoder would give errors as:

jsonschema.exceptions.RefResolutionError: Unresolvable JSON pointer: 'definitions/EX_FOO'

Solution

These errors happen because when compiling the JSON schema for an internal JsonSchemaMixin sub-class we don't propagate the "definitions" created using the FieldEncoder.

This way I propose handling this as another case on the _get_field_definitions method.

elael added 3 commits June 4, 2022 19:59
mypy warns about:
```
dataclasses_jsonschema/apispec.py:38: error: Signature of
"schema_helper" incompatible with supertype "BasePlugin"
dataclasses_jsonschema/apispec.py:38: note:      Superclass:
dataclasses_jsonschema/apispec.py:38: note:          def
schema_helper(self, name: str, definition: Dict[Any, Any], **kwargs:
Any) -> Optional[Dict[Any, Any]]
dataclasses_jsonschema/apispec.py:38: note:      Subclass:
dataclasses_jsonschema/apispec.py:38: note:          def [T <:
JsonSchemaMixin] schema_helper(self, name: str, _: Any, schema:
Union[Type[T], Dict[Any, Any], None] = ..., **kwargs: Any) -> Any
Found 1 error in 1 file (checked 5 source files)
```

Which stands correct given that schema_helper would not accept a named
argument called "definition".
To use with objects of external libraries, embedded definitions are
necessary.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant