Skip to content

Surprising behavior in references: component is resolved only when in a different module? #632

@sphaso

Description

@sphaso

Hello,
we stumbled upon a behavior which we find surprising and would like to know if this is expected and if we are breaking any intended pattern or best practice.
I created a small phoenix project to reproduce: https://github.com/sphaso/api_spec_misunderstanding

https://github.com/sphaso/api_spec_misunderstanding/blob/main/lib/apispecmisunderstanding_web/schema/abc.ex Contains two Schemas pointing to what's effectively the same reference (Submodule) except that Working references it from another Elixir module, while NotWorking references it from a submodule of Abc.
Testing this in https://github.com/sphaso/api_spec_misunderstanding/blob/main/test/apispecmisunderstanding_web/schema_test.exs , cast_value of NotWorking gives an UndefinedFunctionError. The following warning is output:

....warning: Unresolved schema module: nil.
Use OpenApiSpex.resolve_schema_modules/1 to resolve modules ahead of time.

  (open_api_spex 3.20.1) lib/open_api_spex.ex:407: OpenApiSpex.resolve_schema/2
  (open_api_spex 3.20.1) lib/open_api_spex/cast/any_of.ex:47: OpenApiSpex.Cast.AnyOf.cast_any_of/3
  (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
  (open_api_spex 3.20.1) lib/open_api_spex/cast/array.ex:59: OpenApiSpex.Cast.Array.cast_array/1
  (open_api_spex 3.20.1) lib/open_api_spex/cast/array.ex:10: OpenApiSpex.Cast.Array.cast/1
  (ex_unit 1.15.4) lib/ex_unit/assertions.ex:785: ExUnit.Assertions.assert_raise/2
  test/apispecmisunderstanding_web/schema_test.exs:10: ApispecmisunderstandingWeb.SchemaTest."test cast_value/2 Not working"/1
  (ex_unit 1.15.4) lib/ex_unit/runner.ex:463: ExUnit.Runner.exec_test/2
  (stdlib 5.0.2) timer.erl:270: :timer.tc/2
  (ex_unit 1.15.4) lib/ex_unit/runner.ex:385: anonymous fn/5 in ExUnit.Runner.spawn_test_monitor/4

Trying to follow the advice gives the same result (see "Also Not Working" test)

However, using the plug (see https://github.com/sphaso/api_spec_misunderstanding/blob/main/lib/apispecmisunderstanding_web/controllers/test_controller.ex and related test) the schema seems to be correctly resolved.

Questions:

  • is it intended behavior that some references types are not resolved depending if they are in the same module or in another one?
  • why is then the plug working? Is there any step we are missing to ensure all references are resolved ahead of time?

I'm happy to help clarifying this issue further if needed. Thank you for your time and attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions