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

AllowClientGeneratedIds for single resource #1299

Closed
verdie-g opened this issue Aug 18, 2023 · 3 comments · Fixed by #1305
Closed

AllowClientGeneratedIds for single resource #1299

verdie-g opened this issue Aug 18, 2023 · 3 comments · Fixed by #1305

Comments

@verdie-g
Copy link
Sponsor Contributor

Is your feature request related to a problem? Please describe.

I would like to create a user for a steam game and reuse their steam id as the user id in my database.

The user is defined as

[Resource]
public class User : Identifiable<long>
{
}

but if I do

POST http://localhost:5203/users
Content-Type: application/vnd.api+json

{
  "data": {
    "type": "users",
    "id": "76561197960279927"
  }
}

I'll get this error

HTTP/1.1 403 Forbidden
Content-Type: application/vnd.api+json
Date: Fri, 18 Aug 2023 20:07:58 GMT
Server: Kestrel
Transfer-Encoding: chunked

{
  "links": {
    "self": "http://localhost:5203/users"
  },
  "errors": [
    {
      "id": "31676f4a-725e-44a3-9bd7-8013c0cf2f88",
      "status": "403",
      "title": "Failed to deserialize request body: The use of client-generated IDs is disabled.",
      "source": {
        "pointer": "/data/id"
      }
    }
  ]
}

I could use the option AllowClientGeneratedIds but it's applied to all resources and I only want it scoped to the user resource.

Describe the solution you'd like

- [Resource]
+ [Resource(AllowClientGeneratedIds = true)]
public class User : Identifiable<long>
{
}

Describe alternatives you've considered

  • Define the steam id as an attribute but every time a steam id is mentioned in-game, I would have to do the mapping to my internal id to understand which users we're talking about
@bkoelman
Copy link
Member

I always wondered why this was only possible globally, and why nobody ever asked for this. It's a legitimate ask, I'm just not sure yet how to best provide that. Probably needs to be stored in the resource graph, settable by an attribute parameter.

Until this is implemented, it would be easiest for you to define a generic resource definition class and register it with open generics. Override the OnPrepareWriteAsync method, checking for the endpoint kind (post) and resource type (user), otherwise throwing when Id is non-default. I'm on vacation right now, so can't provide working code.

@bkoelman
Copy link
Member

bkoelman commented Sep 5, 2023

Hi @verdie-g, this is implemented in #1305. Can you give it a try? Feedback welcome.

@verdie-g
Copy link
Sponsor Contributor Author

verdie-g commented Sep 6, 2023

Looks good to me 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants