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

Issue while using wrap within a JsonResource with same key as within the array #483

Open
jaimyborgman opened this issue Aug 14, 2024 · 0 comments

Comments

@jaimyborgman
Copy link
Contributor

jaimyborgman commented Aug 14, 2024

@romalytvynenko
When using JsonResource responses in combination with $wrap we can make very nice responses; except there is an issue when the $wrap key is set to a value which also contains within the array itself the "wrap" is getting ignored for documentation while the endpoint is still sending it out like so:

the endpoint/backend is providing this:

{
  locale: {
    locale: 'en-NL',
    fallback_locale: 'en-US',
    country: 'NL',
    timezone: 'Europe/Amsterdam',
    currency: 'EUR'
  }
}

the LocaleResource looks like this:

/**
 * @property Locale $resource
 */
class LocaleResource extends ResourceDto
{
    public static $wrap = 'locale';

    public function toArray(Request $request): array
    {
        $resource = $this->resource;

        return [
            'locale' => $this->convertFormat($resource->locale),
            'fallback_locale' => $this->convertFormat($resource->fallbackLocale),
            'country' => $resource->country,
            'timezone' => $resource->timezone,
            'currency' => $resource->currency,
        ];
    }

    private function convertFormat(string $value): string
    {
        return Str::replace('_', '-', $value);
    }
}

when using the same key like above the result is:
image

but when using a different wrapper key like test we get:
image

and if when wrapper key is still locale and the locale key within the array is changed to for instance current_locale we are getting expected results like so:
image

so there is a conflict while having keys with the same name in the data array and wrapper key; then the wrapper key looks to be ignored which shouldn't be the case

if more info is needed please let me know

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

No branches or pull requests

1 participant