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

Nullable required properties during denormalization #794

Open
qdequippe opened this issue Mar 7, 2024 · 0 comments
Open

Nullable required properties during denormalization #794

qdequippe opened this issue Mar 7, 2024 · 0 comments
Labels

Comments

@qdequippe
Copy link

qdequippe commented Mar 7, 2024

Jane version(s) affected: 7.6.0

Description

When a Component Schema contains required fields (see example on how to reproduce) and if these fields are nullable, isset conditions blocks sub object denormalization.

How to reproduce

Giving this component

{
    "BlocAdresse-1": {
        "type": "object",
        "properties": {
            ...
            "indiceRepetition": {
                "pattern": "^([A-Z])$",
                "type": "string",
                "example": "T",
                "description": "GU_ADR011",
                "nullable": true
            },
            ...
            "complementLocalisation": {
                "type": "string",
                "example": "Compl\u00e9ment de localisation",
                "description": "GU_ADR016",
                "nullable": true
            },
            ...
        },
        "required": [
            "indiceRepetition",
            "complementLocalisation"
        ]
    }
}

it generates this code sample on parent model

if (\array_key_exists('adresse', $data) && null !== $data['adresse']) {
    $value_1 = $data['adresse'];
    if (\is_array($data['adresse']) && isset($data['adresse']['indiceRepetition'], $data['adresse']['complementLocalisation'])) {
        $value_1 = $this->denormalizer->denormalize($data['adresse'], BlocAdresse1::class, 'json', $context);
    }
    $object->setAdresse($value_1);
    unset($data['adresse']);
}

Excepts that isset check also null values so denormalization will be never call.

Possible Solution

Replace https://github.com/janephp/janephp/blob/next/src/Component/JsonSchema/Guesser/Guess/ObjectType.php#L71-L76 by

$arrayKeyExistsCondition = new Expr\FuncCall(
    new Name('\array_key_exists'),
    [
        new Arg(new Scalar\String_($key)),
        new Arg($input),
    ]
);

However if $input is not an array this solution is not working.

Additional context

@qdequippe qdequippe added the bug label Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant