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

Feature | JSON flags on JsonBodyRepository #229

Merged
merged 3 commits into from
Jun 6, 2023
Merged

Conversation

Sammyjo20
Copy link
Member

Fixes #194

This PR adds support to customise the JSON flags that are used to encode the JSON body repository in Saloon. For example, if you want to make sure that body that contains slashes doesn't get escaped, you can use the setJsonFlags method on the body method to overwrite them.

They use a "bitmask" which is slightly different from arrays. For example, to set both JSON_THROW_ON_ERROR and JSON_UNESCAPED_SLASHES you would need to separate them with a pipe (|)

$request = new GetServersRequest;
$request->body()->setJsonFlags(JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);

$connector->send($request);

As Saloon recommends keeping logic standardised inside of requests, I would recommend that you set this within the __construct or the boot method of the request...

__construct()
{
     $this->body()->setJsonFlags(JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
}

Side note, @juse-less - any idea why it seems like it's ignoring our baseline file now and spitting out loads of the "ignored" errors?

Copy link
Member Author

@Sammyjo20 Sammyjo20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review

@what-the-diff
Copy link

what-the-diff bot commented Jun 6, 2023

PR Summary 🎉

  • Introducing $jsonFlags for JsonBodyRepository 💡
    Allows you to customize the JSON encoding process.

  • Getter and Setter methods for jsonFlags 🛠️
    For added ease in customizing your JSON encoding experience.

  • Improved error handling with JSON_THROW_ON_ERROR 🚀
    Now throws exceptions during errors instead of staying silent, making debugging a breeze! 😃

@juse-less
Copy link
Contributor

@Sammyjo20 I believe it's because the lock file isn't checked into the repo, so you'll always get the newest PHPStan version.

They recently fixed a couple things on how they interpret callables, so I reckon that's related.

@Sammyjo20
Copy link
Member Author

@juse-less Thanks 👍 I managed to fix it by rebuilding the ignores :)

@Sammyjo20 Sammyjo20 merged commit d7c3e14 into v2 Jun 6, 2023
@Sammyjo20 Sammyjo20 deleted the feature/json-flags branch June 23, 2023 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escaped backslashes or unicode in json
2 participants