Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Larry Gebhardt
    @lgebhardt
    Note, I haven't tested that code. I generally prefer to handle field level checks on the model or the processor.
    Artur
    @artur79
    @lgebhardt thanks for quick reply :) basically what I want to do if user is admin trigger exception when API call tries to change pass, otherwise allow. Maybe I'm just going wrong way and as you've explained just remove it from params
    Larry Gebhardt
    @lgebhardt
    Would excluding the password from the updatable fields for an admin work? Then it would get kicked out earlier in the process with a standard message about not being allowed to update password.
    Lasse Immonen
    @turtleman
    Are there any plans to make a new 0.9 release with the ForceSSL fix included?
    Larry Gebhardt
    @lgebhardt
    @turtleman Yes, I'm planning on getting a new v0.9 release out, however I want to review a few more PRs first.
    I've gone down a rabbit hole with the testing. I'm now reworking the CI testing to use Github Actions instead of Travis.
    Joe Gaudet
    @joegaudet
    @lgebhardt does this address the linkage inclusion issue?
    Lasse Immonen
    @turtleman
    @lgebhardt splendid! thank you so much for the excellent work! :)
    Larry Gebhardt
    @lgebhardt
    @joegaudet I haven't be the time to start on restoring the linkage inclusion, but I should be able to get to it next.
    New release v0.10.4 adds support rails 6.1 and ruby 3
    Joe Gaudet
    @joegaudet
    Awesome thank @lgebhardt
    It’s the last thing blocking me upgrading :)
    Artur
    @artur79
    @lgebhardt I'v tried to call params from before_replace_fields block and got error Internal Server Error: undefined local variable or method `params'
    Artur
    @artur79
    managed to achieve what I'v need with below code, thanks :)
    def replace_fields(field_data)
      if @model.admin? && @model.persisted? && field_data[:attributes] && field_data[:attributes].include?(:password)
        raise JSONAPI::Exceptions::ParameterNotAllowed, 'Admin password change not allowed via API.' and return
      else
        super
      end
    end
    Artur
    @artur79

    Another question, in the User model I have:

      has_many :memberships, dependent: :destroy
      has_many :user_groups, through: :memberships

    than in User resource:

      has_many :user_groups, exclude_links: :default
      has_many :memberships, exclude_links: :default

    I call patch user with

    "relationships": {
          "user_groups": {
            "data": [
              {
                "id": "10",
                "type": "user_groups"
              }
            ]
          }

    Problem is that when user already has some user_groups assigned they are deleted. Is there any way to keep existing relationships ? Without hacking replace_fields method

    RobL
    @braindeaf

    Morning.. was just about to create a bug and came here first. Perhaps I'm not getting the documentation but ...I'd like to create a singular resource.

     class Api::V1::MeResource < JSONAPI::Resource
      model_name 'User'
    
      singleton singleton_key: (lambda do |context|
        key = context[:current_user].try(:id)
        raise(JSONAPI::Exceptions::RecordNotFound, nil) if key.nil?
    
        key
      end)
    
      attributes :uuid, :full_name, :email
    end

    and this works to an extent. However the response is not what I would like or expect.

    {
      "data": {
        "id": "sdfldsflndfnxxxxxxxxxx",
        "type": "mes",
        "links": {
          "self": "http://demo.yeahwhatever.test/api/v1/mes"
        },
        "attributes": {
          "uuid": "c568d2b5-132a-4d5e-b025-b0443dddc8ee",
          "full_name": "Mr Rob Lacey",
          "email": "rob.lacey@yeahwhatever.co.uk"
        }
      }
    }

    I need the type to be 'users' and the self to be /api/v1/me

    can anyone help? I'm crawling from method to method in the gem and it all seems pretty rigid. I'll settle for /users/me but I need the response to be slim compared to /users/:id which is more detailed.

    Thank you in advance :)

    DarianAnjuhal
    @DarianAnjuhal
    Hi guys, I have two DateTime fields (end and start) in my Model "LiveStream". I want to get/post/... like this attributes: { end: {time: "xx:xx", date: "2021-02-11"} ...
    What would be the best practice? Do I have to create a DateTimeResource and add relationships like: has_one :start, class_name: 'DateTime' or do I need a specific Serializer to achieve that?
    Would be nice if you can me point to the correct direction
    Larry Gebhardt
    @lgebhardt

    @artur79 The spec says we should be performing a complete replacement https://jsonapi.org/format/#crud-updating-resource-relationships

    If a relationship is provided in the relationships member of a resource object in a PATCH request, its value MUST be a relationship object with a data member. The relationship’s value will be replaced with the value specified in this member.

    Likewise for updating just the relationships, from https://jsonapi.org/format/#crud-updating-to-many-relationships

    If a client makes a PATCH request to a URL from a to-many relationship link, the server MUST either completely replace every member of the relationship, return an appropriate error response if some resources can not be found or accessed, or return a 403 Forbidden response if complete replacement is not allowed by the server.

    Artur
    @artur79
    @lgebhardt got it, so it's just following jsonapi standard
    so, if I want to block it in the situation that some relationships exist, I need to check it and return 403 exception in before_update in resource class ?
    Larry Gebhardt
    @lgebhardt
    @artur79 If you want to deviate from the standard I'd consider overriding the BasicResource._replace_to_many_links method. I think this might work:
        def _replace_to_many_links(relationship_type, relationship_key_values, options)
          relationship = self.class._relationship(relationship_type)
          existing = find_related_ids(relationship, options)
          super(relationship_type, relationship_key_values & existing, options
        end
    You could block it by raising an exception in an override of _replace_to_many_links
    The override could be in your base resource if you want the whole app to behave that way, or in just one specific resource.
    You would lose the ability to clear the relationships with an empty array, but you could also check for that and modify the logic slightly
    Artur
    @artur79
    @lgebhardt why replace_to_many_links not replace_to_many_relationship ? basically I want to return exception 403 according to the standard, if some relationships already exist
    Larry Gebhardt
    @lgebhardt

    @braindeaf I've handled that with an id translation in the users controller like:

    class UsersController < BaseController
      before_action :convert_me_id
    
      def convert_me_id
        if params[:id] && params[:id].casecmp('me') == 0
          params[:id] = context[:current_user].id
          context[:me] = true
        end
      end
    end

    This just uses the UserResource and not a MeResource. You could modify the params or the context in the case of an id using me to alter the response. The methods like fetchable_fields, updatable_fields all have access to the context either as an argument or on the resource instance.

    RobL
    @braindeaf
    ah ok @lgebhardt that could work.... I'm going to give that a whirl.
    thank you :)
    Larry Gebhardt
    @lgebhardt
    @artur79 replace_to_many_relationship is a method on the processor. Other operation types also replace the relationships. The resource's _replace_to_many_links is the easiest place to put an override that should get them all.
    Artur
    @artur79
    thanks for explanation :)
    Artur
    @artur79
    here's my hack, maybe some other users will need it. Thanks again @lgebhardt :)
      def _replace_to_many_links(relationship_type, relationship_key_values, options)
        if relationship_type == :user_groups && @model.user_groups.any?
          fail JSONAPI::Exceptions::ToManySetReplacementForbidden.new(detail: 'User already has user group(s) assigned.')
        end
    
        super(relationship_type, relationship_key_values, options)
      end
    Ayanga
    @aok-solutions
    is there a way to add extra attributes (on create) that are not on the model or a relationship? keep running into the Param not allowed error
    Larry Gebhardt
    @lgebhardt
    @aok-solutions You can add attributes to the resource that don't exist in the model. Add the attribute as normal and then add a getter and setter.
    Ayanga
    @aok-solutions
    @lgebhardt thanks!
    Joe Gaudet
    @joegaudet
    @lgebhardt any idea what kind of timeline on have the linkage included, I definitely appreciate that you’re likely quite busy, just putting together a roadmap for the next two quarters.
    Larry Gebhardt
    @lgebhardt
    @joegaudet I should be able to at least look into what it will take by the end of next week. Hopefully it's a simple thing and I can have a fix then too.
    Joe Gaudet
    @joegaudet
    Awesome man, very much appreciated.
    Anuj Patel
    @patelanuj28
    Upgraded to 0.10.4 and started seeing this 500 server error in the included property.
    [2021-02-22 20:21:14 UTC] P42065R1 ERROR Internal Server Error: undefined method downcase' for nil:NilClass ruby-2.7.2@manager-ruby272/gems/jsonapi-resources-0.10.4/lib/jsonapi/relationship.rb:66:inblock (3 levels) in polymorphic_types'
    /Users/anujp/.rvm/gems/ruby-2.7.2@manager-ruby272/gems/jsonapi-resources-0.10.4/lib/jsonapi/relationship.rb:65:in each' /Users/anujp/.rvm/gems/ruby-2.7.2@manager-ruby272/gems/jsonapi-resources-0.10.4/lib/jsonapi/relationship.rb:65:inblock (2 levels) in polymorphic_types'
    Any one has seen this issue?
    benebrice
    @benebrice
    Did you change key_type default value?
    Anuj Patel
    @patelanuj28
    Nope, I haven't changed anything it is working with 0.9.0, so I am guessing it's regression with 0.10.4
    Benjamin Fleischer
    @bf4
    Yes. I even have a fa8ling
    Failing pr. Been broken for years
    Anuj Patel
    @patelanuj28
    Does 0.10.4 supports include on polymorphism property like include=image.taggable?
    Anuj Patel
    @patelanuj28
    After monkey patching find_related_polymorphic_fragments with following change relation_position = relation_positions[row[2].underscore.pluralize] and updating association in the model. It started working.
    benebrice
    @benebrice
    Hi ! Do you think an integration of Goldiloader could be a good idea? This might solve a lot of time on optimisation with nested relationships...