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

Custom dto output return tons of @context #6810

Open
HrjSnz opened this issue Nov 20, 2024 · 12 comments
Open

Custom dto output return tons of @context #6810

HrjSnz opened this issue Nov 20, 2024 · 12 comments
Labels

Comments

@HrjSnz
Copy link

HrjSnz commented Nov 20, 2024

API Platform version(s) affected: 4.0.7 +

Description
With custom resource and use parameter "output" for custom dto return in response full "@ context" . In version 4.0.6 and below not.
If not use "output" everything works fine.

How to reproduce
Just make custom output DTO.

Possible Solution
Is this bug ? I couldn't find any mention of this in the changes.

Additional Context
custom api resource
image

in response tons of "@ context" (around 1k lines)
image

@soyuka
Copy link
Member

soyuka commented Nov 22, 2024

mark your DTO as an API Resource to avoid that, I'll check if we can do something.

@HrjSnz
Copy link
Author

HrjSnz commented Nov 22, 2024

Hi, thanks for tip, but does not work for me. Still getting around 1k lines context. Updated and tested on 4.0.9. Tried add #[ApiResource] to all related DTOs and still not work.

image

@Gist-race
Copy link

I encountered a similar issue, and I managed to resolve it by adding the #[ApiPlatform\Metadata\ApiResource] attribute to my DTO and registering it in the api_platform.yaml configuration file under the mapping section. After making these changes, everything worked as expected.

@HrjSnz
Copy link
Author

HrjSnz commented Nov 28, 2024

Wow, thanks. It works. I configured mapping and resource_class_directories and it works. But it's interesting, it wasn't needed before. Nowhere in the changes is there any mention of this necessary configuration. Until 4.0.6 everything was fine. Anyway, thank you

@soyuka maybe add it to doc if this change persist ? :)

@HrjSnz HrjSnz closed this as completed Nov 28, 2024
@HrjSnz HrjSnz reopened this Nov 28, 2024
@HrjSnz
Copy link
Author

HrjSnz commented Nov 28, 2024

@soyuka @Gist-race so.. i just tested it again and i mentioned context is correct but now documentation is not correct. DTO is now as resource in doc which is wrong. For fix it need write #[ApiResource( operations: [] )] . This is to much writing for fix context response. Something must have changed in 4.0.7 that affects this

image

@soyuka
Copy link
Member

soyuka commented Nov 29, 2024

let's keep that as a bug I need to investigate but it's not that easy to fix, we need to host this context and refer to it on the api

@soyuka soyuka added the bug label Nov 29, 2024
soyuka added a commit to soyuka/core that referenced this issue Dec 1, 2024
@soyuka
Copy link
Member

soyuka commented Dec 1, 2024

@HrjSnz I was not able to reproduce your issue, could you help with a full reproducer?

see soyuka@f97eaed it does return:

^ array:4 [
  "@context" => array:3 [
    "@vocab" => "http://localhost/docs.jsonld#"
    "hydra" => "http://www.w3.org/ns/hydra/core#"
    "foo" => "Output/foo"
  ]
  "@type" => "Output"
  "@id" => "/.well-known/genid/b9cb24ad0035053a4802"
  "foo" => "bar"
]

I'm using the last 4.x version available.

@soyuka soyuka removed the bug label Dec 1, 2024
@HrjSnz
Copy link
Author

HrjSnz commented Dec 4, 2024

@soyuka Hello, i am sorry lack of time. I will try it tomorrow. Just what i see in your test you use

GetCollection()

but my problem is with output on

Post()

I will try it tomorrow and provide more information. Thanks

@HrjSnz
Copy link
Author

HrjSnz commented Dec 5, 2024

@soyuka Hello, i think i found problem. Reason why test is without error is in

There is default value true

'serializer' => [
'hydra_prefix' => true,
],

In

->arrayNode('serializer')
->addDefaultsIfNotSet()
->children()
->booleanNode('hydra_prefix')->defaultFalse()->info('Use the "hydra:" prefix.')->end()
->end()
->end()
is as default value false

So all test run with 'hydra_prefix' => true but default configuration bundle is 'hydra_prefix' => false.

Place where is wrong set context is here

if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {
return [HYDRA_CONTEXT, $context];
}

There in test in $this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] is true but in app is default false so result is opposite in real app instead in test.

another indicator is api_platform.yaml. In my api_platform.yaml if let it default so getting this wrong context. If i use

api_platform:
    serializer:
        hydra_prefix: true

so problem gone and it return correct context

And it started in 4.0.7 because #6765 has changed context from one line to full array. Before it always jumped wrong but got one line context.

@SherinBloemendaal
Copy link

SherinBloemendaal commented Dec 11, 2024

@soyuka @Gist-race so.. i just tested it again and i mentioned context is correct but now documentation is not correct. DTO is now as resource in doc which is wrong. For fix it need write #[ApiResource( operations: [] )] . This is to much writing for fix context response. Something must have changed in 4.0.7 that affects this

image

I also ran in to the same issue. What I usually do is instead of marking it as an complete #[ApiResource]
I use a single #[Get] attribute:
#[Get(controller: NotFoundAction::class, openapi: false, output: false, read: false)]

This way it won't be shown in the docs and calling the endpoint returns a not found :)

@HrjSnz
Copy link
Author

HrjSnz commented Dec 11, 2024

@SherinBloemendaal yes, but before it works without it. And write additional endpoint and close it is in my opinion bad practice. As i mentioned above, i probably found the error, but then no one responds to it. Problem is not context itself but if statement which is called wrong.

@soyuka
Copy link
Member

soyuka commented Dec 16, 2024

I managed to reproduce I'm looking into this.

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

4 participants