问:
How can I bulk insert records with UUID primary keys using insert_all in Rails?
I have a controller action which, upon creation of a parent object, generates and saves a batch of M-M associations. The database is Postgres and all relevant tables in the application use UUIDs.

When I use insert_all! on this batch of records the insert fails with ArgumentError (No unique index found for id). I assume this is because I'm not providing IDs for the records I'm trying to insert, but I don't understand how I'm supposed to be able to.

From my understanding, gen_random_uuid() is a Postgres function, not a Rails one. I understand that insert_all! and similar functions skip Rails callbacks but gen_random_uuid() is set as the default value for these records at the Postgres level, so why is this not being respected? Surely I don't have to manually provide UUIDs for each record?

If I do have to manually provide the UUIDs, how do I? Is there a corresponding function in Rails which generates the same type of UUID? I've come across SecureRandom.uuid but the UUIDs generated by this function do not appear to be the same as the ones generated by Postgres.

All examples I can find in the docs, on this website and in blogs use basic IDs, so haven't been helpful. I would also like to avoid adding more gems if possible - this smells like something that should be built into Rails.

Not sure if this is relevent but the database is tenanted, so tables use tenant_id and id composite primary keys.

In summary my question is twofold:

Is there a way to tell insert_all!, ActiveRecord or Postgres to respect the gen_random_uuid() default, so that the UUIDs can be generated automatically by the database (as they are in all other record creation scenarios)?

If not, how can I generate suitable UUIDs in Rails to assign to the records?

Thank you in advance.

Notes

Migration:

create_table :foo, primary_key: [:tenant_id, :id] do |t|
  t.uuid :id, default: -> { "gen_random_uuid()" }, null: false
  t.references :tenant, null: false, index: false
  t.references :foo, null: false, type: :uuid
  t.references :bar, null: false, type: :uuid
  t.timestamps

  t.index :id, unique: true
end
```
<p><code>structure.sql</code> (autogenerated by Rails upon migration):</p>
<pre class="lang-sql prettyprint-override"><code>CREATE TABLE public.foo (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id bigint NOT NULL,
    foo_id uuid NOT NULL,
    bar_id uuid NOT NULL,
    created_at timestamp(6) without time zone NOT NULL,
    updated_at timestamp(6) without time zone NOT NULL
);

ALTER TABLE ONLY public.assignments
    ADD CONSTRAINT assignments_pkey PRIMARY KEY (tenant_id, id);

CREATE INDEX index_assignments_on_foo_id ON public.assignments USING btree (foo_id);

CREATE INDEX index_assignments_on_bar_id ON public.assignments USING btree (bar_id);

CREATE INDEX index_assignments_on_id ON public.assignments USING btree (id);
</code>```

Controller:

def create
  @foo = Foo.new(foo_params)
  authorize(@foo)

  if @foo.save
    scope = {
      # Attribute values to determine scope
    }
    timestamp = Time.zone.now
    assignments = Bar.where(scope).pluck(:id).map do |bid|
        {
          foo_id: @foo.id,
          bar_id: bid,
          created_at: timestamp,
          updated_at: timestamp,
        }
    end

    Assignment.insert_all!(assignments)
    render turbo_stream: [turbo_stream_foos, flash_notification(:success, "Successfully created foo")]
  else
    render turbo_stream: [turbo_stream_foo_new_form, flash_notification(:error, "Error creating foo")]
  end
end
```
<p>Full trace:</p>
```No unique index found for id:
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/insert_all.rb:124:in `find_unique_index_for'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/insert_all.rb:31:in `initialize'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/persistence.rb:213:in `new'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/persistence.rb:213:in `insert_all!'
  /home/careportal/app/controllers/foos_controller.rb:37:in `create'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:215:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/rendering.rb:53:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:234:in `block in process_action'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:118:in `block in run_callbacks'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/core_ext/time/zones.rb:66:in `use_zone'
  /home/careportal/app/controllers/concerns/core/use_time_zone.rb:11:in `use_time_zone'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
  /usr/local/bundle/gems/active_decorator-1.4.0/lib/active_decorator/view_context.rb:37:in `block (3 levels) in <module:Filter>'
  /usr/local/bundle/gems/active_decorator-1.4.0/lib/active_decorator/view_context.rb:25:in `run_with'
  /usr/local/bundle/gems/active_decorator-1.4.0/lib/active_decorator/view_context.rb:36:in `block (2 levels) in <module:Filter>'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `instance_exec'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
  /usr/local/bundle/gems/actiontext-7.0.4/lib/action_text/rendering.rb:20:in `with_renderer'
  /usr/local/bundle/gems/actiontext-7.0.4/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `instance_exec'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:138:in `run_callbacks'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:233:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/rescue.rb:22:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `block in instrument'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `instrument'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:66:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:151:in `process'
  /usr/local/bundle/gems/actionview-7.0.4/lib/action_view/rendering.rb:39:in `process'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal.rb:188:in `dispatch'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal.rb:251:in `dispatch'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:32:in `serve'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:50:in `block in serve'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `each'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `serve'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:852:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/static.rb:23:in `call'
  /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:36:in `block in call'
  /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:34:in `catch'
  /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:34:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/tempfile_reaper.rb:15:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/etag.rb:27:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/conditional_get.rb:40:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/head.rb:12:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/http/permissions_policy.rb:38:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/http/content_security_policy.rb:36:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/session/abstract/id.rb:266:in `context'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/session/abstract/id.rb:260:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/cookies.rb:696:in `call'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/migration.rb:603:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:99:in `run_callbacks'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/executor.rb:14:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
  /usr/local/bundle/gems/better_errors-2.9.1/lib/better_errors/middleware.rb:62:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:132:in `call_app'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:19:in `block in call'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:17:in `catch'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:17:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/rack/logger.rb:40:in `call_app'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `block in call'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `block in tagged'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:37:in `tagged'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `tagged'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `call'
  /usr/local/bundle/gems/sprockets-rails-3.4.2/lib/sprockets/rails/quiet_assets.rb:13:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/request_id.rb:26:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/method_override.rb:24:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/runtime.rb:22:in `call'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/server_timing.rb:61:in `block in call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/server_timing.rb:26:in `collect_events'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/server_timing.rb:60:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/executor.rb:14:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/static.rb:23:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/sendfile.rb:110:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/host_authorization.rb:137:in `call'
  /usr/local/bundle/gems/rack-mini-profiler-3.0.0/lib/mini_profiler/profiler.rb:393:in `call'
  /usr/local/bundle/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/engine.rb:530:in `call'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/configuration.rb:252:in `call'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/request.rb:77:in `block in handle_request'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/thread_pool.rb:340:in `with_force_shutdown'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/request.rb:76:in `handle_request'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/server.rb:443:in `process_client'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/thread_pool.rb:147:in `block in spawn_thread'
```

Ruby version: 3.1.2

Rails version: 7.0.3

Body:
I have a controller action which, upon creation of a parent object, generates and saves a batch of M-M associations. The database is Postgres and all relevant tables in the application use UUIDs.

When I use insert_all! on this batch of records the insert fails with ArgumentError (No unique index found for id). I assume this is because I'm not providing IDs for the records I'm trying to insert, but I don't understand how I'm supposed to be able to.

From my understanding, gen_random_uuid() is a Postgres function, not a Rails one. I understand that insert_all! and similar functions skip Rails callbacks but gen_random_uuid() is set as the default value for these records at the Postgres level, so why is this not being respected? Surely I don't have to manually provide UUIDs for each record?

If I do have to manually provide the UUIDs, how do I? Is there a corresponding function in Rails which generates the same type of UUID? I've come across SecureRandom.uuid but the UUIDs generated by this function do not appear to be the same as the ones generated by Postgres.

All examples I can find in the docs, on this website and in blogs use basic IDs, so haven't been helpful. I would also like to avoid adding more gems if possible - this smells like something that should be built into Rails.

Not sure if this is relevent but the database is tenanted, so tables use tenant_id and id composite primary keys.

In summary my question is twofold:

Is there a way to tell insert_all!, ActiveRecord or Postgres to respect the gen_random_uuid() default, so that the UUIDs can be generated automatically by the database (as they are in all other record creation scenarios)?

If not, how can I generate suitable UUIDs in Rails to assign to the records?

Thank you in advance.

Notes

Migration:

create_table :foo, primary_key: [:tenant_id, :id] do |t|
  t.uuid :id, default: -> { "gen_random_uuid()" }, null: false
  t.references :tenant, null: false, index: false
  t.references :foo, null: false, type: :uuid
  t.references :bar, null: false, type: :uuid
  t.timestamps

  t.index :id, unique: true
end

structure.sql (autogenerated by Rails upon migration):

CREATE TABLE public.foo (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id bigint NOT NULL,
    foo_id uuid NOT NULL,
    bar_id uuid NOT NULL,
    created_at timestamp(6) without time zone NOT NULL,
    updated_at timestamp(6) without time zone NOT NULL
);

ALTER TABLE ONLY public.assignments
    ADD CONSTRAINT assignments_pkey PRIMARY KEY (tenant_id, id);

CREATE INDEX index_assignments_on_foo_id ON public.assignments USING btree (foo_id);

CREATE INDEX index_assignments_on_bar_id ON public.assignments USING btree (bar_id);

CREATE INDEX index_assignments_on_id ON public.assignments USING btree (id);

Controller:

def create
  @foo = Foo.new(foo_params)
  authorize(@foo)

  if @foo.save
    scope = {
      # Attribute values to determine scope
    }
    timestamp = Time.zone.now
    assignments = Bar.where(scope).pluck(:id).map do |bid|
        {
          foo_id: @foo.id,
          bar_id: bid,
          created_at: timestamp,
          updated_at: timestamp,
        }
    end

    Assignment.insert_all!(assignments)
    render turbo_stream: [turbo_stream_foos, flash_notification(:success, "Successfully created foo")]
  else
    render turbo_stream: [turbo_stream_foo_new_form, flash_notification(:error, "Error creating foo")]
  end
end

Full trace:

No unique index found for id:
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/insert_all.rb:124:in `find_unique_index_for'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/insert_all.rb:31:in `initialize'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/persistence.rb:213:in `new'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/persistence.rb:213:in `insert_all!'
  /home/careportal/app/controllers/foos_controller.rb:37:in `create'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:215:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/rendering.rb:53:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:234:in `block in process_action'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:118:in `block in run_callbacks'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/core_ext/time/zones.rb:66:in `use_zone'
  /home/careportal/app/controllers/concerns/core/use_time_zone.rb:11:in `use_time_zone'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
  /usr/local/bundle/gems/active_decorator-1.4.0/lib/active_decorator/view_context.rb:37:in `block (3 levels) in <module:Filter>'
  /usr/local/bundle/gems/active_decorator-1.4.0/lib/active_decorator/view_context.rb:25:in `run_with'
  /usr/local/bundle/gems/active_decorator-1.4.0/lib/active_decorator/view_context.rb:36:in `block (2 levels) in <module:Filter>'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `instance_exec'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
  /usr/local/bundle/gems/actiontext-7.0.4/lib/action_text/rendering.rb:20:in `with_renderer'
  /usr/local/bundle/gems/actiontext-7.0.4/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `instance_exec'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:138:in `run_callbacks'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:233:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/rescue.rb:22:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `block in instrument'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `instrument'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:66:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:151:in `process'
  /usr/local/bundle/gems/actionview-7.0.4/lib/action_view/rendering.rb:39:in `process'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal.rb:188:in `dispatch'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_controller/metal.rb:251:in `dispatch'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:32:in `serve'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:50:in `block in serve'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `each'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `serve'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:852:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/static.rb:23:in `call'
  /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:36:in `block in call'
  /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:34:in `catch'
  /usr/local/bundle/gems/warden-1.2.9/lib/warden/manager.rb:34:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/tempfile_reaper.rb:15:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/etag.rb:27:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/conditional_get.rb:40:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/head.rb:12:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/http/permissions_policy.rb:38:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/http/content_security_policy.rb:36:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/session/abstract/id.rb:266:in `context'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/session/abstract/id.rb:260:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/cookies.rb:696:in `call'
  /usr/local/bundle/gems/activerecord-7.0.4/lib/active_record/migration.rb:603:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:99:in `run_callbacks'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/executor.rb:14:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
  /usr/local/bundle/gems/better_errors-2.9.1/lib/better_errors/middleware.rb:62:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:132:in `call_app'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:19:in `block in call'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:17:in `catch'
  /usr/local/bundle/gems/web-console-4.2.0/lib/web_console/middleware.rb:17:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/rack/logger.rb:40:in `call_app'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `block in call'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `block in tagged'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:37:in `tagged'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `tagged'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `call'
  /usr/local/bundle/gems/sprockets-rails-3.4.2/lib/sprockets/rails/quiet_assets.rb:13:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/request_id.rb:26:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/method_override.rb:24:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/runtime.rb:22:in `call'
  /usr/local/bundle/gems/activesupport-7.0.4/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/server_timing.rb:61:in `block in call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/server_timing.rb:26:in `collect_events'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/server_timing.rb:60:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/executor.rb:14:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/static.rb:23:in `call'
  /usr/local/bundle/gems/rack-2.2.5/lib/rack/sendfile.rb:110:in `call'
  /usr/local/bundle/gems/actionpack-7.0.4/lib/action_dispatch/middleware/host_authorization.rb:137:in `call'
  /usr/local/bundle/gems/rack-mini-profiler-3.0.0/lib/mini_profiler/profiler.rb:393:in `call'
  /usr/local/bundle/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
  /usr/local/bundle/gems/railties-7.0.4/lib/rails/engine.rb:530:in `call'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/configuration.rb:252:in `call'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/request.rb:77:in `block in handle_request'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/thread_pool.rb:340:in `with_force_shutdown'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/request.rb:76:in `handle_request'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/server.rb:443:in `process_client'
  /usr/local/bundle/gems/puma-5.6.5/lib/puma/thread_pool.rb:147:in `block in spawn_thread'

Ruby version: 3.1.2

Rails version: 7.0.3


来源: stackexchange

问:
Upload multiple images to Storage at once
I'm new to Android development and I have a direct question. I'm trying to save multiple images in Firebase storage in one task and get the respective DownlaodUrl. When I apply the code below, I get an error message.

java.lang.ClassCastException: com.google.firebase.storage.UploadTask$TaskSnapshot cannot be cast to com.google.firebase.storage.UploadTask

Follow implementation I use

import com.google.firebase.storage.UploadTask

```<code> val tasks = mutableListOf<UploadTask>()

 var uploadTask = spaceRef.putBytes(data)

 tasks.add(uploadTask)

 Tasks.whenAllSuccess<UploadTask>(tasks).addOnSuccessListener { uploadTasks ->
        val downloadUrls = mutableListOf<Uri>()

        lifecycleScope.launch {
            uploadTasks.forEach {
                downloadUrls.add(it.await().storage.downloadUrl.await())
            }
        }

 }
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I'm new to Android development and I have a direct question. I'm trying to save multiple images in Firebase storage in one task and get the respective DownlaodUrl. When I apply the code below, I get an error message.

java.lang.ClassCastException: com.google.firebase.storage.UploadTask$TaskSnapshot cannot be cast to com.google.firebase.storage.UploadTask

Follow implementation I use

import com.google.firebase.storage.UploadTask

 val tasks = mutableListOf<UploadTask>()

 var uploadTask = spaceRef.putBytes(data)

 tasks.add(uploadTask)

 Tasks.whenAllSuccess<UploadTask>(tasks).addOnSuccessListener { uploadTasks ->
        val downloadUrls = mutableListOf<Uri>()

        lifecycleScope.launch {
            uploadTasks.forEach {
                downloadUrls.add(it.await().storage.downloadUrl.await())
            }
        }

 }


问:
How to query JSONB column with nested array & objects in Citus postgres
I have a Postgres Table "Organization" containing "email_details" column of JSONB data.
This column will hold data in below format.

```<code>{
    "emails":
     [
      {
        "recipients": ["abc_email@dummy.com", "xyz_email@dummy.com"],
        "email_body": "test1",
      },
      {
        "recipients": ["abc_email@dummy.com"],
        "email_body": "test2",
      },
      {
        //"recipients": [""],
        "email_body": "test3",
      },
    ],
    "other_data": { ... }
}
</code>```

Scenario 1.
I would like to get the list of emails for a particular user using Select query.

For Example,

a). If I send recipient id as : "abc_email@dummy.com" in WHERE Clause, it should return me emails array containing first two objects only.

b). If I send recipient id as : "xyz_email@dummy.com" in WHERE Clause, it should return me emails array containing first object only.

Scenario 2.
I would like to get the entire email_data column if the email_id passed in WHERE clause is present for any one of the recipients in the emails array.

For Example,

a). If I send recipient id as : "xyz_email@dummy.com" in WHERE Clause, it should return me entire email_data as it is.

Kindly suggest appropriate Postgres SELECT query.

Body:
I have a Postgres Table "Organization" containing "email_details" column of JSONB data.
This column will hold data in below format.

{
    "emails":
     [
      {
        "recipients": ["abc_email@dummy.com", "xyz_email@dummy.com"],
        "email_body": "test1",
      },
      {
        "recipients": ["abc_email@dummy.com"],
        "email_body": "test2",
      },
      {
        //"recipients": [""],
        "email_body": "test3",
      },
    ],
    "other_data": { ... }
}

Scenario 1.
I would like to get the list of emails for a particular user using Select query.

For Example,

a). If I send recipient id as : "abc_email@dummy.com" in WHERE Clause, it should return me emails array containing first two objects only.

b). If I send recipient id as : "xyz_email@dummy.com" in WHERE Clause, it should return me emails array containing first object only.

Scenario 2.
I would like to get the entire email_data column if the email_id passed in WHERE clause is present for any one of the recipients in the emails array.

For Example,

a). If I send recipient id as : "xyz_email@dummy.com" in WHERE Clause, it should return me entire email_data as it is.

Kindly suggest appropriate Postgres SELECT query.


问:
Traefik - Why getting connection refused error with ACME
I am trying to setup a traefik proxy and and an nginx static website behind the proxy. Unfortunately I am not able to access the nginx website via https since I am running into issues with getting certificates via letsencrypt.

The traefik proxy will run with the following docker-compose.yml

```<code>version: '3'

services:
  traefik:
    image: traefik:v2.9
    container_name: 001-traefik
    command:
      - "--log.level=DEBUG"
      - "--providers.docker"
      - "--providers.docker.exposedByDefault=false"
      - "--entrypoints.http.address=:80"
      - "--entrypoints.http.http.redirections.entrypoint.to=https"
      - "--entrypoints.http.http.redirections.entrypoint.scheme=https"
      - "--entrypoints.https.address=:443"
      - "--entrypoints.https.http.tls.certResolver=le"
      - "--certificatesresolvers.le.acme.tlschallenge=true"
      - "--certificatesresolvers.le.acme.email=email@email.de"
      - "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json"

    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt/acme.json:/letsencrypt/acme.json
      - ./traefik.yml:/traefik.yml
    networks:
      - proxy

networks:
  proxy:
    external: true
</code>```

After starting the nginx container I can find the following in the logs of the traefik container:

```<code>time="2023-01-10T11:32:02Z" level=debug msg="Adding route for site1.myserver.de with TLS options default" entryPointName=https
time="2023-01-10T11:32:02Z" level=debug msg="Trying to challenge certificate for domain [site1.myserver.de] found in HostSNI rule" rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory"
time="2023-01-10T11:32:02Z" level=debug msg="Looking for provided certificate(s) to validate [\"site1.myserver.de\"]..." rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory"
time="2023-01-10T11:32:02Z" level=debug msg="Domains [\"site1.myserver.de\"] need ACME certificates generation for domains \"site1.myserver.de\"." providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory" rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker
time="2023-01-10T11:32:02Z" level=debug msg="Loading ACME certificates [site1.myserver.de]..." routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory" rule="Host(`site1.myserver.de`)"
time="2023-01-10T11:32:07Z" level=debug msg="Serving default certificate for request: \"site1.myserver.de\""
time="2023-01-10T11:32:07Z" level=debug msg="http: TLS handshake error from 172.20.0.1:34672: remote error: tls: bad certificate"
time="2023-01-10T11:32:07Z" level=debug msg="Building ACME client..." providerName=le.acme
time="2023-01-10T11:32:07Z" level=debug msg="https://acme-v02.api.letsencrypt.org/directory" providerName=le.acme
time="2023-01-10T11:32:07Z" level=error msg="Unable to obtain ACME certificate for domains \"site1.myserver.de\": cannot get ACME client get directory at 'https://acme-v02.api.letsencrypt.org/directory': Get \"https://acme-v02.api.letsencrypt.org/directory\": dial tcp:
lookup acme-v02.api.letsencrypt.org on 127.0.0.11:53: read udp 127.0.0.1:55896->127.0.0.11:53: read: connection refused" rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory"
</code>```

Any idea why I am getting this "connection refused" error message? There is no firewall or anything else blocking the traffic to this environment.

Body:
I am trying to setup a traefik proxy and and an nginx static website behind the proxy. Unfortunately I am not able to access the nginx website via https since I am running into issues with getting certificates via letsencrypt.

The traefik proxy will run with the following docker-compose.yml

version: '3'

services:
  traefik:
    image: traefik:v2.9
    container_name: 001-traefik
    command:
      - "--log.level=DEBUG"
      - "--providers.docker"
      - "--providers.docker.exposedByDefault=false"
      - "--entrypoints.http.address=:80"
      - "--entrypoints.http.http.redirections.entrypoint.to=https"
      - "--entrypoints.http.http.redirections.entrypoint.scheme=https"
      - "--entrypoints.https.address=:443"
      - "--entrypoints.https.http.tls.certResolver=le"
      - "--certificatesresolvers.le.acme.tlschallenge=true"
      - "--certificatesresolvers.le.acme.email=email@email.de"
      - "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json"

    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt/acme.json:/letsencrypt/acme.json
      - ./traefik.yml:/traefik.yml
    networks:
      - proxy

networks:
  proxy:
    external: true

After starting the nginx container I can find the following in the logs of the traefik container:

time="2023-01-10T11:32:02Z" level=debug msg="Adding route for site1.myserver.de with TLS options default" entryPointName=https
time="2023-01-10T11:32:02Z" level=debug msg="Trying to challenge certificate for domain [site1.myserver.de] found in HostSNI rule" rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory"
time="2023-01-10T11:32:02Z" level=debug msg="Looking for provided certificate(s) to validate [\"site1.myserver.de\"]..." rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory"
time="2023-01-10T11:32:02Z" level=debug msg="Domains [\"site1.myserver.de\"] need ACME certificates generation for domains \"site1.myserver.de\"." providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory" rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker
time="2023-01-10T11:32:02Z" level=debug msg="Loading ACME certificates [site1.myserver.de]..." routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory" rule="Host(`site1.myserver.de`)"
time="2023-01-10T11:32:07Z" level=debug msg="Serving default certificate for request: \"site1.myserver.de\""
time="2023-01-10T11:32:07Z" level=debug msg="http: TLS handshake error from 172.20.0.1:34672: remote error: tls: bad certificate"
time="2023-01-10T11:32:07Z" level=debug msg="Building ACME client..." providerName=le.acme
time="2023-01-10T11:32:07Z" level=debug msg="https://acme-v02.api.letsencrypt.org/directory" providerName=le.acme
time="2023-01-10T11:32:07Z" level=error msg="Unable to obtain ACME certificate for domains \"site1.myserver.de\": cannot get ACME client get directory at 'https://acme-v02.api.letsencrypt.org/directory': Get \"https://acme-v02.api.letsencrypt.org/directory\": dial tcp:
lookup acme-v02.api.letsencrypt.org on 127.0.0.11:53: read udp 127.0.0.1:55896->127.0.0.11:53: read: connection refused" rule="Host(`site1.myserver.de`)" routerName=https-nginx@docker providerName=le.acme ACME CA="https://acme-v02.api.letsencrypt.org/directory"

Any idea why I am getting this "connection refused" error message? There is no firewall or anything else blocking the traffic to this environment.


问:
My app returns 403 error although config seems to work properly
I created jwt security config in my spring-boot app and always when I have token in Authorization header added my app returns 403. I created this config basen on this app.

Here's my SecurityConfiguration:

```<code>@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {

  private final JwtAuthenticationFilter jwtAuthFilter;
  private final AuthenticationProvider authenticationProvider;

  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .csrf()
        .disable()
        .authorizeHttpRequests()
        .requestMatchers("/v1/auth/**")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .authenticationProvider(authenticationProvider)
        .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

    return http.build();
  }
}

</code>```

JwtAuthenticationFilter

```<code>@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

  private final JwtService jwtService;
  private final UserDetailsService userDetailsService;

  @Override
  protected void doFilterInternal(
      @NonNull HttpServletRequest request,
      @NonNull HttpServletResponse response,
      @NonNull FilterChain filterChain
  ) throws ServletException, IOException {
    final String authHeader = request.getHeader("Authorization");
    final String jwt;
    final String userEmail;
    if (authHeader == null ||!authHeader.startsWith("Bearer ")) {
      filterChain.doFilter(request, response);
      return;
    }
    jwt = authHeader.substring(7);
    userEmail = jwtService.extractUsername(jwt);
    if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
      UserDetails userDetails = this.userDetailsService.loadUserByUsername(userEmail);
      if (jwtService.isTokenValid(jwt, userDetails)) {
        UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
            userDetails,
            null,
            userDetails.getAuthorities()
        );
        authToken.setDetails(
            new WebAuthenticationDetailsSource().buildDetails(request)
        );
        SecurityContextHolder.getContext().setAuthentication(authToken);
      }
    }
    filterChain.doFilter(request, response);
  }
}
</code>```

ApplicationConfig

```<code>@Configuration
@RequiredArgsConstructor
public class ApplicationConfig {

  private final UserRepository repository;

  @Bean
  public UserDetailsService userDetailsService() {
    return username -> repository.findByEmail(username)
        .orElseThrow(() -> new UsernameNotFoundException("User not found"));
  }

  @Bean
  public AuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userDetailsService());
    authProvider.setPasswordEncoder(passwordEncoder());
    return authProvider;
  }

  @Bean
  public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
    return config.getAuthenticationManager();
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

}
</code>```

JwtService

```<code>@Service
public class JwtService {

  private static final String SECRET_KEY = "secretkeyhere";

  public String extractUsername(String token) {
    return extractClaim(token, Claims::getSubject);
  }

  public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
    final Claims claims = extractAllClaims(token);
    return claimsResolver.apply(claims);
  }

  public String generateToken(UserDetails userDetails) {
    return generateToken(new HashMap<>(), userDetails);
  }

  public String generateToken(
      Map<String, Object> extraClaims,
      UserDetails userDetails
  ) {
    return Jwts
        .builder()
        .setClaims(extraClaims)
        .setSubject(userDetails.getUsername())
        .setIssuedAt(new Date(System.currentTimeMillis()))
        .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24))
        .signWith(getSignInKey(), SignatureAlgorithm.HS256)
        .compact();
  }

  public boolean isTokenValid(String token, UserDetails userDetails) {
    final String username = extractUsername(token);
    return (username.equals(userDetails.getUsername())) && !isTokenExpired(token);
  }

  private boolean isTokenExpired(String token) {
    return extractExpiration(token).before(new Date());
  }

  private Date extractExpiration(String token) {
    return extractClaim(token, Claims::getExpiration);
  }

  private Claims extractAllClaims(String token) {
    return Jwts
        .parserBuilder()
        .setSigningKey(getSignInKey())
        .build()
        .parseClaimsJws(token)
        .getBody();
  }

  private Key getSignInKey() {
    byte[] keyBytes = Decoders.BASE64.decode(SECRET_KEY);
    return Keys.hmacShaKeyFor(keyBytes);
  }
}
</code>```

To test it I send GET request to http://localhost:8080/v1/example in postman with Bearer token... in Authorization header. Response was 403 Forbidden.

Here's controller class.

ExampleController

```<code>@RestController
@RequestMapping("/v1/example")
public class ExampleController {

    @GetMapping
    public String getTestMessage() {
        return "test message";
    }
}
</code>```

I tried changing configuration, checking authentication, test all methods, but everything seems to work properly

Body:
I created jwt security config in my spring-boot app and always when I have token in Authorization header added my app returns 403. I created this config basen on this app.

Here's my SecurityConfiguration:

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {

  private final JwtAuthenticationFilter jwtAuthFilter;
  private final AuthenticationProvider authenticationProvider;

  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .csrf()
        .disable()
        .authorizeHttpRequests()
        .requestMatchers("/v1/auth/**")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .authenticationProvider(authenticationProvider)
        .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

    return http.build();
  }
}

JwtAuthenticationFilter

@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

  private final JwtService jwtService;
  private final UserDetailsService userDetailsService;

  @Override
  protected void doFilterInternal(
      @NonNull HttpServletRequest request,
      @NonNull HttpServletResponse response,
      @NonNull FilterChain filterChain
  ) throws ServletException, IOException {
    final String authHeader = request.getHeader("Authorization");
    final String jwt;
    final String userEmail;
    if (authHeader == null ||!authHeader.startsWith("Bearer ")) {
      filterChain.doFilter(request, response);
      return;
    }
    jwt = authHeader.substring(7);
    userEmail = jwtService.extractUsername(jwt);
    if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
      UserDetails userDetails = this.userDetailsService.loadUserByUsername(userEmail);
      if (jwtService.isTokenValid(jwt, userDetails)) {
        UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
            userDetails,
            null,
            userDetails.getAuthorities()
        );
        authToken.setDetails(
            new WebAuthenticationDetailsSource().buildDetails(request)
        );
        SecurityContextHolder.getContext().setAuthentication(authToken);
      }
    }
    filterChain.doFilter(request, response);
  }
}

ApplicationConfig

@Configuration
@RequiredArgsConstructor
public class ApplicationConfig {

  private final UserRepository repository;

  @Bean
  public UserDetailsService userDetailsService() {
    return username -> repository.findByEmail(username)
        .orElseThrow(() -> new UsernameNotFoundException("User not found"));
  }

  @Bean
  public AuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userDetailsService());
    authProvider.setPasswordEncoder(passwordEncoder());
    return authProvider;
  }

  @Bean
  public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
    return config.getAuthenticationManager();
  }

  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

}

JwtService

@Service
public class JwtService {

  private static final String SECRET_KEY = "secretkeyhere";

  public String extractUsername(String token) {
    return extractClaim(token, Claims::getSubject);
  }

  public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
    final Claims claims = extractAllClaims(token);
    return claimsResolver.apply(claims);
  }

  public String generateToken(UserDetails userDetails) {
    return generateToken(new HashMap<>(), userDetails);
  }

  public String generateToken(
      Map<String, Object> extraClaims,
      UserDetails userDetails
  ) {
    return Jwts
        .builder()
        .setClaims(extraClaims)
        .setSubject(userDetails.getUsername())
        .setIssuedAt(new Date(System.currentTimeMillis()))
        .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24))
        .signWith(getSignInKey(), SignatureAlgorithm.HS256)
        .compact();
  }

  public boolean isTokenValid(String token, UserDetails userDetails) {
    final String username = extractUsername(token);
    return (username.equals(userDetails.getUsername())) && !isTokenExpired(token);
  }

  private boolean isTokenExpired(String token) {
    return extractExpiration(token).before(new Date());
  }

  private Date extractExpiration(String token) {
    return extractClaim(token, Claims::getExpiration);
  }

  private Claims extractAllClaims(String token) {
    return Jwts
        .parserBuilder()
        .setSigningKey(getSignInKey())
        .build()
        .parseClaimsJws(token)
        .getBody();
  }

  private Key getSignInKey() {
    byte[] keyBytes = Decoders.BASE64.decode(SECRET_KEY);
    return Keys.hmacShaKeyFor(keyBytes);
  }
}

To test it I send GET request to http://localhost:8080/v1/example in postman with Bearer token... in Authorization header. Response was 403 Forbidden.

Here's controller class.

ExampleController

@RestController
@RequestMapping("/v1/example")
public class ExampleController {

    @GetMapping
    public String getTestMessage() {
        return "test message";
    }
}

I tried changing configuration, checking authentication, test all methods, but everything seems to work properly


问:
how to us the assetPrefix option of the next-images npm package
I am using the next-images package in my nextJS application. (https://www.npmjs.com/package/next-images)

The documentation says:

```<code>You can serve remote images by setting assetPrefix option
</code>```

However when I set the assetPrefix option in the next.config.js file, the whole next JS app stops working.

Becuase it adds the prefix to absolutly everything:

If I don't use the assetPrefix option and put the image file locally everything works.

I want to be able to serve a remote image from my component like this:

```<code>    <section className="text-center section-34 section-sm-60 section-md-top-50 section-md-bottom-50 bg-image-breadcrumbs"
        style={{ backgroundImage: "url(" + `${require("http://asorockfood.local/media/0v0ahz21/bg-image-breadcrumbs-default.png")}` + ")"}}>
</code>```

Am I miss understanding how this feature works?

This is what my next.config.js looks like:

```<code>module.exports = withImages({
    assetPrefix: 'http://asorockfood.local',
    dynamicAssetPrefix: true,
    reactStrictMode: true,
    images: {
            disableStaticImages: true,
            remotePatterns: [
            {
                protocol: "http",
                hostname: "**",
            },
            {
                protocol: "https",
                hostname: "**",
            },
        ],
    },
    env: {
        customSnipcartJS: fs.readFileSync('public/custom-snipcart.js').toString(),
        snipcartInstallJS: fs.readFileSync('public/snipcart-install.js').toString()
    }
})
</code>```

Body:
I am using the next-images package in my nextJS application. (https://www.npmjs.com/package/next-images)

The documentation says:

You can serve remote images by setting assetPrefix option

However when I set the assetPrefix option in the next.config.js file, the whole next JS app stops working.

Becuase it adds the prefix to absolutly everything:

If I don't use the assetPrefix option and put the image file locally everything works.

I want to be able to serve a remote image from my component like this:

    <section className="text-center section-34 section-sm-60 section-md-top-50 section-md-bottom-50 bg-image-breadcrumbs"
        style={{ backgroundImage: "url(" + `${require("http://asorockfood.local/media/0v0ahz21/bg-image-breadcrumbs-default.png")}` + ")"}}>

Am I miss understanding how this feature works?

This is what my next.config.js looks like:

module.exports = withImages({
    assetPrefix: 'http://asorockfood.local',
    dynamicAssetPrefix: true,
    reactStrictMode: true,
    images: {
            disableStaticImages: true,
            remotePatterns: [
            {
                protocol: "http",
                hostname: "**",
            },
            {
                protocol: "https",
                hostname: "**",
            },
        ],
    },
    env: {
        customSnipcartJS: fs.readFileSync('public/custom-snipcart.js').toString(),
        snipcartInstallJS: fs.readFileSync('public/snipcart-install.js').toString()
    }
})


问:
Log4Net XXE-attack vulnerability
I am maintaining a ASP.NET Framework 4.6 application that uses Log4Net (version 1.2.10.0) to store logs. I read that it has XXE vulnerabilities, so I am thinking if I need to upgrade it.

As I understand the XXE attacks the user need to be able to upload a XML-configuration to the application? But my web app only use a XML-configuration that is within the app and are not public or accessable to the user. Do I still need to update the Log4net version? I am asking because I do not own the code for the app, and might have to require the owner to make the update.

Title: Log4Net XXE-attack vulnerability


问:
Angular change detection SignalR breaks upon disconnect
I'm trying to get SignalR to reconnect correctly after any kind of connection error or otherwise. I have this working. However, once an error has happened, the Angular change detection breaks. SignalR still receives the events, but I have to do any kind of UI interaction (like switching a toggle that doesn't do anything, or pressing any button) to get the UI to update.

I am presuming that the issue lies in error handling, but I'd greatly appreciate some pointers on what the actual issue can be as it is not clear to me how to do this and I can't find any good examples. The scenario I'm testing is simply stopping the IIS service, after which the SignalR connection fails immediately with errors visible in the console. Then when I start it again it reconnects, but the change detection is now broken.

I'm currently using version 7.0.0 of the Microsoft/SignalR client package in Angular 15.

Additional information: it seems like with 6.0.11 I only have this in production branches.


问:
doctrine not able to map embedded correctly
I have embedded type like this

```<code><?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <embeddable name="App\Domain\Entity\ShopId">
        <id name="value" type="uuid" column="id" length="36" />
    </embeddable>
</doctrine-mapping>
</code>```

and it's added to main entity mapping like this

```<code><?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity
            name="App\Domain\Entity\Shop"
            table="shops"
            repository-class="App\Domain\Entity\Infrastructure\Doctrine\Repository\ShopRepository">

        <embedded name="id" class="App\Domain\Entity\ShopId" use-column-prefix="false" />
     // other fields
     //....
    </entity>

</doctrine-mapping>
</code>```

ShopId class looks like this:

```<code> class ShopId
{
    public function __construct(protected string $value)
    {
        if (
            preg_match(
                '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/',
                $value,
            ) !== 1
        ) {
            throw new InvalidId();
        }
    }

    public function value(): string
    {
        return $this->value;
    }

    public function equals(self $id): bool
    {
        return $this->value === $id->value();
    }
}

</code>```

and added to Shop entity like this:

```<code>class Shop implements AggregateRoot
{
    public function __construct(
        private readonly ShopId $id,
        // other data
       // ...
    }
</code>```

And for some reason some times my tests failing, I would say that it's failing once per 30-40 runs but still, and it's failing with error like this

```<code>Stacktrace
App\Tests\Integration\Infrastructure\Doctrine\Repository\ShopRepositoryTest::testGetShop
Error: Object of class App\Domain\Entity\ShopId could not be converted to string
/srv/app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:3160
/srv/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:480
/srv/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:197
/srv/app/src/App/Infrastructure/Doctrine/Repository/ShopRepository.php:24
/srv/app/tests/Integration//Infrastructure/Doctrine/Repository/ShopRepositoryTest.php:42
</code>```

My test looks like

```<code>    public function testGetShop(): void
    {
        $shopId = new ShopId((string) new UuidV4());

        $shop = new Shop($shopId); // I'm skipping other data here
        $repo = self::getContainer()->get(ShopRepository::class);
        $repo->save($shop);

        $em = self::getContainer()->get(EntityManagerInterface::class);
        assert($em instanceof EntityManagerInterface);
        $em->clear();

        $foundShop = $repo->findShop($shopId);

        self::assertTrue($foundShop->equals($shop));
    }
</code>```

and my repo

```<code>final class ShopRepository extends ServiceEntityRepositor
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Shop::class);
    }

    public function findShop(ShopId $id): ?Shop
    {
        $shop = $this->find($id);

        return $shop;
    }

    public function save(Shop $shop): void
    {
        $this->_em->persist($shop);
        $this->_em->flush();
    }
}
</code>```

So in most cases it's mapping okay, I would say in 99%, but some times doctrine not able to map it correctly. Maybe I'm stupid or blind, but I do not know how to fix it, thank in advance.

Body:
I have embedded type like this

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <embeddable name="App\Domain\Entity\ShopId">
        <id name="value" type="uuid" column="id" length="36" />
    </embeddable>
</doctrine-mapping>

and it's added to main entity mapping like this

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity
            name="App\Domain\Entity\Shop"
            table="shops"
            repository-class="App\Domain\Entity\Infrastructure\Doctrine\Repository\ShopRepository">

        <embedded name="id" class="App\Domain\Entity\ShopId" use-column-prefix="false" />
     // other fields
     //....
    </entity>

</doctrine-mapping>

ShopId class looks like this:

 class ShopId
{
    public function __construct(protected string $value)
    {
        if (
            preg_match(
                '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/',
                $value,
            ) !== 1
        ) {
            throw new InvalidId();
        }
    }

    public function value(): string
    {
        return $this->value;
    }

    public function equals(self $id): bool
    {
        return $this->value === $id->value();
    }
}

and added to Shop entity like this:

class Shop implements AggregateRoot
{
    public function __construct(
        private readonly ShopId $id,
        // other data
       // ...
    }

And for some reason some times my tests failing, I would say that it's failing once per 30-40 runs but still, and it's failing with error like this

Stacktrace
App\Tests\Integration\Infrastructure\Doctrine\Repository\ShopRepositoryTest::testGetShop
Error: Object of class App\Domain\Entity\ShopId could not be converted to string
/srv/app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:3160
/srv/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:480
/srv/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:197
/srv/app/src/App/Infrastructure/Doctrine/Repository/ShopRepository.php:24
/srv/app/tests/Integration//Infrastructure/Doctrine/Repository/ShopRepositoryTest.php:42

My test looks like

    public function testGetShop(): void
    {
        $shopId = new ShopId((string) new UuidV4());

        $shop = new Shop($shopId); // I'm skipping other data here
        $repo = self::getContainer()->get(ShopRepository::class);
        $repo->save($shop);

        $em = self::getContainer()->get(EntityManagerInterface::class);
        assert($em instanceof EntityManagerInterface);
        $em->clear();

        $foundShop = $repo->findShop($shopId);

        self::assertTrue($foundShop->equals($shop));
    }

and my repo

final class ShopRepository extends ServiceEntityRepositor
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Shop::class);
    }

    public function findShop(ShopId $id): ?Shop
    {
        $shop = $this->find($id);

        return $shop;
    }

    public function save(Shop $shop): void
    {
        $this->_em->persist($shop);
        $this->_em->flush();
    }
}

So in most cases it's mapping okay, I would say in 99%, but some times doctrine not able to map it correctly. Maybe I'm stupid or blind, but I do not know how to fix it, thank in advance.


问:
Create History via single file and without edited my created model
All.

I want track history of my whole admin panel via LARAVEL. And, already my project was done so now I don't want to go for change each and every model file like don't want to change model event or don't want create observer or trait.

So I have already tried with Trait, Observer and also via model event.
Using those option I have to add / update my code for each and every module so, Without change all files. How can I do that?


问:
Cannot push image to Docker Hub - no proxy configured
Newbie to Docker.Just learning it currently.
I am on Ubuntu 20.04 LTS.
I face the below error while pushing the sample node image that i have created.
What can be the issue?
i already have pushed 2 images recently to my dockerhub
i have created this image using Dockerfile.
i am already logged in to docker using docker login command.
Pushing this image through Docker Desktop also gives the same error.

"https://registry-1.docker.io/v2/rishi56/node_app/blobs/uploads/": dialing registry-1.docker.io:443 no proxy configured: connecting to 3.216.34.172:443: dial tcp 3.216.34.172:443: i/o timeout

my Dockerfile -

FROM node:slim
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD node index.js

i have tried to restart Docker Desktop and also my PC.
I am on Ubuntu 20.04 LTS.


问:
/document/document:1000012662:open failed: ENOENT (No such file or directory)
I was trying to upload a file(pdf, zip, etc). But I got an error while I pick a file /document/document:1000012662:open failed: ENOENT (No such file or directory). Please find my code below. Below code is working fine when I upload images and videos not able to upload other files. I gave access all permission too.

AndroidManifest.xml

```<code> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/anna_university_logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@drawable/mylogo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:replace="android:icon"
        tools:targetApi="m">

</application>
</code>```

MainActivity.java

```<code>Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "Choose a file"), 2 );
</code>```

To enable all files access

```<code>if (Build.VERSION.SDK_INT >= 30){
            if (!Environment.isExternalStorageManager()) {
                Toast.makeText(this, "Please Enable access all files", Toast.LENGTH_LONG).show();

                try {
                    Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
                    Intent getpermission = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
                    startActivity(getpermission);

                } catch (Exception e) {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                    startActivity(intent);

                }
            }
        }
</code>```
```<code>@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode !=RESULT_CANCELED){

            switch (requestCode){
                case 0:
                    if(resultCode == RESULT_OK && data !=null){
                        Bitmap image = (Bitmap) data.getExtras().get("data");
                        selectedImage = FileUtils.getPath(MainActivity.this, getImageUri(MainActivity.this,image));
                        uploadFileToServer();
                    }
                    break;
                case 1:
                case 2:
                case 4:
                case 3:
                    if(resultCode == RESULT_OK && data !=null){

                        Uri image = data.getData();

                        selectedImage = FileUtils.getPath(MainActivity.this,image);

                        uploadFileToServer();
                    }
                    break;

            }

        }
    }
</code>```
```<code>
    public void uploadFileToServer(){

        File file = new File(Uri.parse(selectedImage).getPath());
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData(
                "sendfile",file.getName(),
                requestBody);

        ApiInterface service = RetrofitUploadBuilder.getClient().create(ApiInterface.class);

        Call<FileUploadModel> call = service.performUploadAFile(filePart);
        call.enqueue(new Callback<FileUploadModel>() {
            @Override
            public void onResponse(Call<FileUploadModel> call, Response<FileUploadModel> response) {
                loadingdialog.dismissdialog();
                if (response.isSuccessful()) {

                }
                else
                {

                }
            }

            @Override
            public void onFailure(Call<FileUploadModel> call, Throwable t) {

               track.setText(t.getMessage());

            }
        });

    }

</code>```

Please give a solution. Advance thanks

Body:
I was trying to upload a file(pdf, zip, etc). But I got an error while I pick a file /document/document:1000012662:open failed: ENOENT (No such file or directory). Please find my code below. Below code is working fine when I upload images and videos not able to upload other files. I gave access all permission too.

AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/anna_university_logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@drawable/mylogo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:replace="android:icon"
        tools:targetApi="m">

</application>

MainActivity.java

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "Choose a file"), 2 );

To enable all files access

if (Build.VERSION.SDK_INT >= 30){
            if (!Environment.isExternalStorageManager()) {
                Toast.makeText(this, "Please Enable access all files", Toast.LENGTH_LONG).show();

                try {
                    Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
                    Intent getpermission = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
                    startActivity(getpermission);

                } catch (Exception e) {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                    startActivity(intent);

                }
            }
        }

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode !=RESULT_CANCELED){

            switch (requestCode){
                case 0:
                    if(resultCode == RESULT_OK && data !=null){
                        Bitmap image = (Bitmap) data.getExtras().get("data");
                        selectedImage = FileUtils.getPath(MainActivity.this, getImageUri(MainActivity.this,image));
                        uploadFileToServer();
                    }
                    break;
                case 1:
                case 2:
                case 4:
                case 3:
                    if(resultCode == RESULT_OK && data !=null){

                        Uri image = data.getData();

                        selectedImage = FileUtils.getPath(MainActivity.this,image);

                        uploadFileToServer();
                    }
                    break;

            }

        }
    }

    public void uploadFileToServer(){

        File file = new File(Uri.parse(selectedImage).getPath());
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData(
                "sendfile",file.getName(),
                requestBody);

        ApiInterface service = RetrofitUploadBuilder.getClient().create(ApiInterface.class);

        Call<FileUploadModel> call = service.performUploadAFile(filePart);
        call.enqueue(new Callback<FileUploadModel>() {
            @Override
            public void onResponse(Call<FileUploadModel> call, Response<FileUploadModel> response) {
                loadingdialog.dismissdialog();
                if (response.isSuccessful()) {

                }
                else
                {

                }
            }

            @Override
            public void onFailure(Call<FileUploadModel> call, Throwable t) {

               track.setText(t.getMessage());

            }
        });

    }

Please give a solution. Advance thanks


问:
Power Query M language Script Error 500 Internal Server error
Am trying to call the api url to generate token using the Power Query M language
for some reason the script generates the error 500 internal server error

Code:

```<code>//body is a variable where we stored information that will be sent on request
//body is a variable where are stored information that will be sent on request

let body = "{

""Clientid"": " & tokenClientId & ",

""Secret"": " & tokenSecret & ",

""GrantType"": " & grantType & "

}",

//GetToken is a function- it was created to make the request

getToken = Json.Document(

Web.Contents(

//tokenAddress is the variable where the address tokes was set

tokenAddress,

//In content, the body variable is inserted with all information needed

[

Headers = [#"Content-Type"= "application/json"], //Required by documentation

Content = Text.ToBinary(body)]

)

),

token = getToken[idToken]

in getToken
</code>```

I have tried to pass the token address without using the variable and it was still failed

Body:
Am trying to call the api url to generate token using the Power Query M language
for some reason the script generates the error 500 internal server error

Code:

//body is a variable where we stored information that will be sent on request
//body is a variable where are stored information that will be sent on request

let body = "{

""Clientid"": " & tokenClientId & ",

""Secret"": " & tokenSecret & ",

""GrantType"": " & grantType & "

}",

//GetToken is a function- it was created to make the request

getToken = Json.Document(

Web.Contents(

//tokenAddress is the variable where the address tokes was set

tokenAddress,

//In content, the body variable is inserted with all information needed

[

Headers = [#"Content-Type"= "application/json"], //Required by documentation

Content = Text.ToBinary(body)]

)

),

token = getToken[idToken]

in getToken

I have tried to pass the token address without using the variable and it was still failed


问:
How to properly make generic a usage of TypeAdapterFactory and TypeAdapter using Retrofit and Dagger2 in Android Kotlin?
Step by step, I'll try to explain my situation, because I already wasted 2 days, but not getting the expected solution.

So, I'm not giving you any specific scenario, because use cases may change.

Problem: I want to use Gson TypeAdapterFactory and TypeAdapter using my existing structure which is implemented using MVVM, Retrofit and Dagger2.

Notes: I want to do for entire project so, TypeAdapterFactory and TypeAdapter should be generic type. Or let's say generic type approach is complicated, then how about handle different type of API responses?

Example1: For the same key, depending on use cases, JSONObject may come or JSONArray also. Please note the key which I'm talking about that may inside some nested data class.
Example2: A key is coming to double, but I want that in dataclass it should store as abc.toInt()

Like that there are many use cases are there in project, so I want to use TypeAdapterFactory and TypeAdapter.

Please note: I don't want to use JSONDeseriliazer, because that is old approach and few limitations.

Code:

```<code>@Provides
    @Singleton
    fun provideCartGsonConverterFactory(): GsonConverterFactory {
        val customGson: Gson = GsonBuilder()
            .registerTypeAdapter(CartResponse::class.java, CustomTypeAdapter::class.java)
            .registerTypeAdapterFactory(CustomTypeAdapterFactory())
            .create()

        return GsonConverterFactory.create(customGson)

    }
</code>```

CustomTypeAdapter:

```<code>class CustomTypeAdapter<T> (val classOfT: Class<in T>) : TypeAdapter<T>() {
    private val gson: Gson = Gson()

      override fun write(jsonWriter: JsonWriter?,  value: T) {
          gson.toJson(classOfT, OfferItem::class.java, jsonWriter)
      }

    override fun read(jsonReader: JsonReader): T {
        val locations: T = null
        jsonReader.beginObject()
        jsonReader.nextName()
        if (jsonReader.peek() === JsonToken.BEGIN_ARRAY) {
           /* locations = T(
                gson.fromJson(
                    jsonReader,
                    Array<T>::class.java
                ) as Array<Item?>
            )*/
        } else if (jsonReader.peek() === JsonToken.BEGIN_OBJECT) {

        } else {
            throw JsonParseException("Unexpected token " + jsonReader.peek())
        }
        jsonReader.endObject()
        return T
    }

}
</code>```

CustomTypeAdapterFactory:

```<code>import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken

class CustomTypeAdapterFactory : TypeAdapterFactory {
    override fun <T> create(gson: Gson?, type: TypeToken<T>): TypeAdapter<T>? {
        val rawType = type.rawType
        return if (!rawType.isEnum) null else CustomTypeAdapter(rawType)
    }
}
</code>```

I think something like this? Retrofit/Gson how to create type adapters dynamically

I appreciate all answers, let me know if I can explain you better.

Body:
Step by step, I'll try to explain my situation, because I already wasted 2 days, but not getting the expected solution.

So, I'm not giving you any specific scenario, because use cases may change.

Problem: I want to use Gson TypeAdapterFactory and TypeAdapter using my existing structure which is implemented using MVVM, Retrofit and Dagger2.

Notes: I want to do for entire project so, TypeAdapterFactory and TypeAdapter should be generic type. Or let's say generic type approach is complicated, then how about handle different type of API responses?

Example1: For the same key, depending on use cases, JSONObject may come or JSONArray also. Please note the key which I'm talking about that may inside some nested data class.
Example2: A key is coming to double, but I want that in dataclass it should store as abc.toInt()

Like that there are many use cases are there in project, so I want to use TypeAdapterFactory and TypeAdapter.

Please note: I don't want to use JSONDeseriliazer, because that is old approach and few limitations.

Code:

@Provides
    @Singleton
    fun provideCartGsonConverterFactory(): GsonConverterFactory {
        val customGson: Gson = GsonBuilder()
            .registerTypeAdapter(CartResponse::class.java, CustomTypeAdapter::class.java)
            .registerTypeAdapterFactory(CustomTypeAdapterFactory())
            .create()

        return GsonConverterFactory.create(customGson)

    }

CustomTypeAdapter:

class CustomTypeAdapter<T> (val classOfT: Class<in T>) : TypeAdapter<T>() {
    private val gson: Gson = Gson()

      override fun write(jsonWriter: JsonWriter?,  value: T) {
          gson.toJson(classOfT, OfferItem::class.java, jsonWriter)
      }

    override fun read(jsonReader: JsonReader): T {
        val locations: T = null
        jsonReader.beginObject()
        jsonReader.nextName()
        if (jsonReader.peek() === JsonToken.BEGIN_ARRAY) {
           /* locations = T(
                gson.fromJson(
                    jsonReader,
                    Array<T>::class.java
                ) as Array<Item?>
            )*/
        } else if (jsonReader.peek() === JsonToken.BEGIN_OBJECT) {

        } else {
            throw JsonParseException("Unexpected token " + jsonReader.peek())
        }
        jsonReader.endObject()
        return T
    }

}

CustomTypeAdapterFactory:

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken

class CustomTypeAdapterFactory : TypeAdapterFactory {
    override fun <T> create(gson: Gson?, type: TypeToken<T>): TypeAdapter<T>? {
        val rawType = type.rawType
        return if (!rawType.isEnum) null else CustomTypeAdapter(rawType)
    }
}

I think something like this? Retrofit/Gson how to create type adapters dynamically

I appreciate all answers, let me know if I can explain you better.


问:
Simple HTML DOM: How do I scrape only the main title, image src and body of the webpage in PHP?
The thing is that I want to scrape data from a public webpage, and it's not just the complete data, I want to scrape just the main title, image src and body of the webpage, and not the other unnecessary data (such as the advertisement images, text, sign up/feedback form data, etc present in the webpage).

I am using Simple HTML DOM library for this purpose:

```<code>$html = file_get_html('my url here');

foreach($html->find('img') as $element)
    echo $element->src . '<br>';

echo $html->find('title',0)->plaintext;

foreach($html->find('p') as $element){
    echo $element->plaintext;
}
</code>```

The code above, fetches all the data present in the webpage, including all the unnecessary ad images and text. I want to fetch only the data that is relevant to the webpage url. Is there a way to do that in the library or do I have to use a separate logic to extract only the relevant data?

How do I proceed? Please guide me

Body:
The thing is that I want to scrape data from a public webpage, and it's not just the complete data, I want to scrape just the main title, image src and body of the webpage, and not the other unnecessary data (such as the advertisement images, text, sign up/feedback form data, etc present in the webpage).

I am using Simple HTML DOM library for this purpose:

$html = file_get_html('my url here');

foreach($html->find('img') as $element)
    echo $element->src . '
';

echo $html->find('title',0)->plaintext;

foreach($html->find('p') as $element){
    echo $element->plaintext;
}

The code above, fetches all the data present in the webpage, including all the unnecessary ad images and text. I want to fetch only the data that is relevant to the webpage url. Is there a way to do that in the library or do I have to use a separate logic to extract only the relevant data?

How do I proceed? Please guide me


问:
python eli5 show_weights() show nothing
In my program,

```<code>from sklearn import linear_model
from keras.wrappers.scikit_learn import KerasClassifier,KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance

if __name__ == '__main__':
    (cnn_tube_par_X_train, cnn_tube_par_X_test, cnn_tube_Y_train, cnn_tube_Y_test) = read_file()
    sc_X = StandardScaler()
    sc_Y = StandardScaler()

    sc_cnn_tube_par_X_train = sc_X.fit_transform(cnn_tube_par_X_train.iloc[:, 1:6].values)
    sc_cnn_tube_par_X_test = sc_X.transform(cnn_tube_par_X_test.iloc[:, 1:6].values)
    sc_cnn_tube_eff_Y_train = sc_Y.fit_transform(cnn_tube_Y_train.iloc[:, -1:].values)
    sc_cnn_tube_eff_Y_test = sc_Y.transform(cnn_tube_Y_test.iloc[:, -1:].values)
    MLR_pImportance(sc_cnn_tube_par_X_train,sc_cnn_tube_par_X_test,sc_cnn_tube_eff_Y_train,sc_cnn_tube_eff_Y_test)
def MLR_pImportance(sc_mlr_tube_par_X_train,sc_mlr_tube_par_X_test,sc_mlr_tube_eff_Y_train,sc_mlr_tube_eff_Y_test):
    mlr = linear_model.LinearRegression()
    mlr.fit(sc_mlr_tube_par_X_train,sc_mlr_tube_eff_Y_train)

    perm = PermutationImportance(mlr,random_state=1).fit(sc_mlr_tube_par_X_test,sc_mlr_tube_eff_Y_test)
    print(perm.feature_importances_)
    print(perm.feature_importances_std_)
    eli5.show_weights(perm)
</code>```

The results show that :

```<code>[0.63895352 0.1270582  0.06904505 0.32131836 0.02549574]
[0.02766096 0.01535046 0.01789114 0.02761288 0.01048179]
</code>```

these are the result of

```<code>print(perm.feature_importances_)
print(perm.feature_importances_std_)
</code>```

but the sebtance:
eli5.show_weights(perm)
show nothing

could you tell the reason,and how to solve it

Title: python eli5 show_weights() show nothing

Body:
In my program,

from sklearn import linear_model
from keras.wrappers.scikit_learn import KerasClassifier,KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance

if __name__ == '__main__':
    (cnn_tube_par_X_train, cnn_tube_par_X_test, cnn_tube_Y_train, cnn_tube_Y_test) = read_file()
    sc_X = StandardScaler()
    sc_Y = StandardScaler()

    sc_cnn_tube_par_X_train = sc_X.fit_transform(cnn_tube_par_X_train.iloc[:, 1:6].values)
    sc_cnn_tube_par_X_test = sc_X.transform(cnn_tube_par_X_test.iloc[:, 1:6].values)
    sc_cnn_tube_eff_Y_train = sc_Y.fit_transform(cnn_tube_Y_train.iloc[:, -1:].values)
    sc_cnn_tube_eff_Y_test = sc_Y.transform(cnn_tube_Y_test.iloc[:, -1:].values)
    MLR_pImportance(sc_cnn_tube_par_X_train,sc_cnn_tube_par_X_test,sc_cnn_tube_eff_Y_train,sc_cnn_tube_eff_Y_test)
def MLR_pImportance(sc_mlr_tube_par_X_train,sc_mlr_tube_par_X_test,sc_mlr_tube_eff_Y_train,sc_mlr_tube_eff_Y_test):
    mlr = linear_model.LinearRegression()
    mlr.fit(sc_mlr_tube_par_X_train,sc_mlr_tube_eff_Y_train)

    perm = PermutationImportance(mlr,random_state=1).fit(sc_mlr_tube_par_X_test,sc_mlr_tube_eff_Y_test)
    print(perm.feature_importances_)
    print(perm.feature_importances_std_)
    eli5.show_weights(perm)

The results show that :

[0.63895352 0.1270582  0.06904505 0.32131836 0.02549574]
[0.02766096 0.01535046 0.01789114 0.02761288 0.01048179]

these are the result of

print(perm.feature_importances_)
print(perm.feature_importances_std_)

but the sebtance:
eli5.show_weights(perm)
show nothing

could you tell the reason,and how to solve it


问:
dx-react-scheduler change 12 hour based datepicker format to 24 hour
i am using '@devexpress/dx-react-scheduler' library.

I need to change the 12 hour am/pm format to a 24 hour based date picker. But i can't seem to solve the issue.

The dateEditorComponent is inside of the "Appointment form".

Documentation

https://devexpress.github.io/devextreme-reactive/react/scheduler/docs/reference/appointment-form/

Heres my code, non usefull code has been removed in example.

           import {
             ViewState,
             EditingState,
             IntegratedEditing,
             SchedulerDateTime,
           } from "@devexpress/dx-react-scheduler";
           import {
             Scheduler,
             Appointments,
             ViewSwitcher,
             AppointmentForm,
           } from "@devexpress/dx-react-scheduler-material-ui";

         //This is the component i need to fix to 24 hour based clock.
        const DateEditor = (props: any) => {
        return (
              <AppointmentForm.DateEditor
                {...props}
                value={props.value}
                locale={"da-DK"}
              ></AppointmentForm.DateEditor>
            );
          };

          const BasicLayout = ({
            onFieldChange,
            appointmentData,
            ...restProps
          }: any) => (
            <AppointmentForm.BasicLayout
              appointmentData={appointmentData}
              onFieldChange={onFieldChange}
              {...restProps}
              locale={"da-DK"}
            ></AppointmentForm.BasicLayout>
          );

          const AppointmentLayout = ({
            onFieldChange,
            appointmentData,
            children,
            ...restProps
          }: any) => (
            <AppointmentForm.Layout
              basicLayoutComponent={BasicLayout}
              {...restProps}
              locale={"da-DK"}
                   >
                {children}
              </AppointmentForm.Layout>
           );

     <Scheduler
        data={schedulerData}
        locale={"da-DK"}
        firstDayOfWeek={1}
      >
        <ViewState
          defaultCurrentDate={currentDate}
        />
        <EditingState onCommitChanges={handleDragDrop} />
        <IntegratedEditing />
        <Appointments appointmentComponent={Appointment} />
        <ViewSwitcher />

        <AppointmentForm
          layoutComponent={AppointmentLayout}
          recurrenceLayoutComponent={() => null}
          booleanEditorComponent={() => null}
          dateEditorComponent={DateEditor}
        />

        <DragDropProvider />
        <DateNavigator />
        <CurrentTimeIndicator shadePreviousCells updateInterval={10000} />
      </Scheduler>
```


问:
React doesn't update components sometimes?
I have a following structure in React, but whenever I click on the buttons, the rendering is not changed:

function App() {
  const [showTxt1, setShowTxt1] = useState(false);
  const [showTxt2, setShowTxt2] = useState(false);

  const setShowButtons = (selected) => {
    console.log("selected:", selected);

    setShowTxt1(selected === "txt1");
    setShowTxt2(selected === "txt2");
  }

  return (
    <React.Fragment>

          <button
            type="button"
            className="btn btn-primary"
            onClick={() => {
              setShowButtons("txt1");
            }}
          >
            Show Txt1
          </button>
          <button
            type="button"
            className="btn btn-primary"
            onClick={() => {
              setShowButtons("txt2");
            }}
          >
           Show Txt2
          </button>

        {
          showTxt1 &&
text1

        }
        {showTxt2 &&
text2

        }

    </React.Fragment>
  );
}

```

I see the log on the console, but the page is not updated. On the 'Components' tab in debugging, I only get an error message: 'Timed out, while inspecting...'

This is a simplified version of my app, where I think the issue is.

Sometimes when I open a new tab (and thus the debugger is closed), it works. What can cause the issue?

Body:
I have a following structure in React, but whenever I click on the buttons, the rendering is not changed:

function App() {
  const [showTxt1, setShowTxt1] = useState(false);
  const [showTxt2, setShowTxt2] = useState(false);

  const setShowButtons = (selected) => {
    console.log("selected:", selected);

    setShowTxt1(selected === "txt1");
    setShowTxt2(selected === "txt2");
  }

  return (
    <React.Fragment>

          <button
            type="button"
            className="btn btn-primary"
            onClick={() => {
              setShowButtons("txt1");
            }}
          >
            Show Txt1
          </button>
          <button
            type="button"
            className="btn btn-primary"
            onClick={() => {
              setShowButtons("txt2");
            }}
          >
           Show Txt2
          </button>

        {
          showTxt1 &&
text1

        }
        {showTxt2 &&
text2

        }

    </React.Fragment>
  );
}

I see the log on the console, but the page is not updated. On the 'Components' tab in debugging, I only get an error message: 'Timed out, while inspecting...'

This is a simplified version of my app, where I think the issue is.

Sometimes when I open a new tab (and thus the debugger is closed), it works. What can cause the issue?


问:
Download videos from a remote site with PyhtonDownload videos from a remote site with Pyhton
Hello I want to download videos from x one site locale with pyhton, when I search the Internet I only download the video at the given address, can you help this process how to pull videos from http traffic while browsing within a single site Hello I want to download videos from a site x locale with pyhton, when I search the Internet, it only downloads the video at the given address, how can you help me pull videos from http traffic while browsing within a single site?</p>


问:
Get db call inside pytest FastAPI
So what I'm trying to do is get the test user that im creating while testing to delete at the end but I get the error: FAILED tests_main.py::test_delete_new_users - AttributeError: 'Depends' object has no attribute 'query'

This is my code:

```<code>def test_delete_new_users(db: Session = Depends(database.get_db)):
    auth = client.post('/token',
                       data={'username': 'test123', 'password': 'test123'}
                       )
    access_token = auth.json().get('access_token')
    user = db.query(models.User).filter(models.User.username == 'test123').first()

    response = client.post('/user/delete/' + user.user_id,
                           headers={
                               'Authorization': 'bearer' + access_token
                           })
    assert response.status_code == 200
</code>```

Do I need to make a separate API call to retrieve this user for instance make an api called get_user_id_with_username where I will pass username as attribute or is there a way where I can make a db call within a pytest because from what I read I can only use Depends on API calls. Thanks in advance

Title: Get db call inside pytest FastAPI

Body:
So what I'm trying to do is get the test user that im creating while testing to delete at the end but I get the error: FAILED tests_main.py::test_delete_new_users - AttributeError: 'Depends' object has no attribute 'query'

This is my code:

def test_delete_new_users(db: Session = Depends(database.get_db)):
    auth = client.post('/token',
                       data={'username': 'test123', 'password': 'test123'}
                       )
    access_token = auth.json().get('access_token')
    user = db.query(models.User).filter(models.User.username == 'test123').first()

    response = client.post('/user/delete/' + user.user_id,
                           headers={
                               'Authorization': 'bearer' + access_token
                           })
    assert response.status_code == 200

Do I need to make a separate API call to retrieve this user for instance make an api called get_user_id_with_username where I will pass username as attribute or is there a way where I can make a db call within a pytest because from what I read I can only use Depends on API calls. Thanks in advance


问:
Retrieving the last record according to date and other record not in the 2nd table in mysql 5.6
I have 2 tables, table1 and table2

table1 id is inserted to table2 lead_id

table1

id
loan_id
status

1
100
valid

2
101
valid

3
102
valid

4
103
valid

table2

id
lead_id
lead_status
created

1
1
REJECTED
01/01/2023

2
2
REJECTED
01/01/2023

3
3
REJECTED
01/01/2023

4
1
REJECTED
10/01/2023

5
2
REJECTED
10/01/2023

6
3
REJECTED
10/01/2023

what is the query to show loan_id that is not there in table2 and last rejected in table2 according to created

result like -

loan_id
created

101
10/01/2023

102
10/01/2023

103
10/01/2023

104
null


问:
unexpected return value, after setjmp
I tried to get the return value in the signal handler function, using the longjmp(), but the program return which is not what I expected. Can someone help me? The code is as follows:

```<code>void SignalHandler(int sig)
{
    longjmp(frameInfo, -2);
}
int main(int argc, char** argv)
{
    signal(SIGSEGV, SignalHandler);
    if(setjmp(frameInfo) == 0) {
        void* ptr = NULL;
        int a = *((int *)ptr);
        printf("%d\n", a);
        return -1;
    } else {
        printf("in \n"); // printf can work
        exit(-2); // or return -2;
    }
    return 1;
}
</code>```

I don't know what happened after the printf.

update:

By check assemble code, I see the expected return value.

```<code>mov    $0xfffffffe,%eax
</code>```

Compile code and generate a.out, use python or shell check return, ret = 254:

```<code>// python
cmd = ./a.out
a = subprocess.Popen(cmd, shell = True)
ret = a.wait()

// shell
./a.out
echo $?
</code>```

My guess is that after the program returned, the operating system did something special. The question is how do I get the real return value?

Title: unexpected return value, after setjmp

Body:
I tried to get the return value in the signal handler function, using the longjmp(), but the program return which is not what I expected. Can someone help me? The code is as follows:

void SignalHandler(int sig)
{
    longjmp(frameInfo, -2);
}
int main(int argc, char** argv)
{
    signal(SIGSEGV, SignalHandler);
    if(setjmp(frameInfo) == 0) {
        void* ptr = NULL;
        int a = *((int *)ptr);
        printf("%d\n", a);
        return -1;
    } else {
        printf("in \n"); // printf can work
        exit(-2); // or return -2;
    }
    return 1;
}

I don't know what happened after the printf.

update:

By check assemble code, I see the expected return value.

mov    $0xfffffffe,%eax

Compile code and generate a.out, use python or shell check return, ret = 254:

// python
cmd = ./a.out
a = subprocess.Popen(cmd, shell = True)
ret = a.wait()

// shell
./a.out
echo $?

My guess is that after the program returned, the operating system did something special. The question is how do I get the real return value?


问:
Typescript yup condition validation
```<code>interface ParamsA {
  a: string
}

const ParamsASchema: yup.ObjectSchema<ParamsA> = yup.object({
  a: yup.string().defined(),
})

interface ParamsB {
  b: string
}

const ParamsBSchema: yup.ObjectSchema<ParamsB> = yup.object({
  b: yup.string().defined(),
})

interface MyParam {
  method: '0' | '1' | '2'
  params?: ParamsA | ParamsB
}

const MyParamSchema: yup.ObjectSchema<MyParam> = yup
  .object({
    method: yup.string().oneOf(['0', '1', '2']).defined(),
    prams: yup.mixed().oneOf([ParamsASchema, ParamsBSchema]).optional(),
  })
  .defined()
</code>```

This does not work. As shown above, i have an interface named MyParam, which has a field named params. "params" can be instance of ParamsA, ParamsB and undefined. How to setup schema for MyParam

Title: Typescript yup condition validation

Body:
interface ParamsA {
  a: string
}

const ParamsASchema: yup.ObjectSchema<ParamsA> = yup.object({
  a: yup.string().defined(),
})

interface ParamsB {
  b: string
}

const ParamsBSchema: yup.ObjectSchema<ParamsB> = yup.object({
  b: yup.string().defined(),
})

interface MyParam {
  method: '0' | '1' | '2'
  params?: ParamsA | ParamsB
}

const MyParamSchema: yup.ObjectSchema<MyParam> = yup
  .object({
    method: yup.string().oneOf(['0', '1', '2']).defined(),
    prams: yup.mixed().oneOf([ParamsASchema, ParamsBSchema]).optional(),
  })
  .defined()

This does not work. As shown above, i have an interface named MyParam, which has a field named params. "params" can be instance of ParamsA, ParamsB and undefined. How to setup schema for MyParam