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

Overridden serialization methods doesn't work in JSON Schema for optional fields and inner dataclasses #139

Closed
Fatal1ty opened this issue Aug 18, 2023 · 0 comments · Fixed by #140
Assignees
Labels
bug Something isn't working

Comments

@Fatal1ty
Copy link
Owner

  • mashumaro version: 3.9
  • Python version: 3.11
  • Operating System: MacOS 13.3.1

Description

When you override serialization method through serialization_strategy or field metadata as noted here for dataclass type or any type that is used inside Optional, this method's return type is not reflected in JSON Schema.

What I Did

First example:

from dataclasses import dataclass
from mashumaro.config import BaseConfig
from mashumaro.jsonschema import build_json_schema

def dataclass_serialize(value) -> str:
    return str(value)

@dataclass
class Bar:
    x: int

@dataclass
class Foo:
    bar: Bar

    class Config(BaseConfig):
        serialization_strategy = {
            Bar: {"serialize": dataclass_serialize},
        }

print(build_json_schema(Foo).to_json())
Actual result
{
    "type": "object",
    "title": "Foo",
    "properties": {
        "bar": {
            "type": "object",
            "title": "Bar",
            "properties": {
                "x": {
                    "type": "integer"
                }
            },
            "additionalProperties": false,
            "required": [
                "x"
            ]
        }
    },
    "additionalProperties": false,
    "required": [
        "bar"
    ]
}
Expected result
{
    "type": "object",
    "title": "Foo",
    "properties": {
        "bar": {
            "type": "string"
        }
    },
    "additionalProperties": false,
    "required": [
        "bar"
    ]
}

Second example:

from typing import Optional
from dataclasses import dataclass
from mashumaro.config import BaseConfig
from mashumaro.jsonschema import build_json_schema

def int_serialize(value) -> str:
    return str(value)

@dataclass
class Foo:
    bar: Optional[int]

    class Config(BaseConfig):
        serialization_strategy = {
            int: {"serialize": int_serialize},
        }

print(build_json_schema(Foo).to_json())
Actual result
{
    "type": "object",
    "title": "Foo",
    "properties": {
        "bar": {
            "anyOf": [
                {
                    "type": "integer"
                },
                {
                    "type": "null"
                }
            ]
        }
    },
    "additionalProperties": false,
    "required": [
        "bar"
    ]
}
Expected result
{
    "type": "object",
    "title": "Foo",
    "properties": {
        "bar": {
            "anyOf": [
                {
                    "type": "string"
                },
                {
                    "type": "null"
                }
            ]
        }
    },
    "additionalProperties": false,
    "required": [
        "bar"
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant