Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing common definitions #243

Open
char0n opened this issue Dec 29, 2019 · 3 comments · May be fixed by #244
Open

Sharing common definitions #243

char0n opened this issue Dec 29, 2019 · 3 comments · May be fixed by #244

Comments

@char0n
Copy link

char0n commented Dec 29, 2019

Is there an idiomatic way how to share Schemas not specific to any controller ? Like Error definition. I found a way how to do it, but not sure if there is a better way/solution.

  @doc false
  def swagger_definitions do
    %{
      ApiToken:
        JsonApi.resource do
          description("Standard pong response")

          attributes do
            revoked(:boolean, "Is the API Token revoked?", default: false)

            revoked_at(:string, "Date and time of API Token revocation",
              format: "date-time",
              default: nil
            )

            description(:string, "Description associated with API Token", default: nil)

            issued_at(:string, "Date and time of API Token issuance",
              format: "date-time",
              default: nil
            )
          end
        end,
      Error: swagger_definitions_common()[:Error]
    }
  end

I have a swagger_definitions_common in different module and I just import the function and call it to give me the Error schema for current controller.

@mbuhot
Copy link
Contributor

mbuhot commented Jan 2, 2020

Your solution seems fine to me.
There’s some docs related to reusing parameters: https://github.com/xerions/phoenix_swagger/blob/master/docs/reusing-swagger-parameters.md

The idea is that the swagger dsl is just sugar over a function pipeline, so you are free to call into your own functions at any point to make use of shared definitions.

@char0n
Copy link
Author

char0n commented Jan 2, 2020

@mbuhot thanks. Only problem with this solution is the following: If I have a module with swagger_definitions_common function that returns common schema definitions, it is not possible to reference schema definitions inside this method.

defmodule ControllerHelpers do
  @moduledoc """
  Conveniences for building controllers.
  """
  use PhoenixSwagger

  @doc """
  Returns map of common swagger definitions.

  These definitions (data structures) are not specific to any controller or
  business domain logic.
  """
  def swagger_definitions_common do
    %{
      ErrorResource:
        swagger_schema do
          title("Error")

          description(
            "Error objects provide additional information about problems encountered while performing an operation."
          )

          properties do
            code(:string, "An application-specific error code, expressed as a string value.",
              required: true
            )

            detail(
              :string,
              "A human-readable explanation specific to this occurrence of the problem",
              required: true
            )

            id(:string, "A unique identifier for this particular occurrence of the problem",
              required: true
            )

            status(
              :string,
              "The HTTP status code applicable to this problem, expressed as a string value",
              required: true
            )

            title(:string, "Error title, usually HTTP reason phrase", required: true)

            source(
              Schema.new do
                properties do
                  parameter(
                    :string,
                    "A string indicating which URI query parameter caused the error",
                    default: nil,
                    required: false
                  )

                  pointer(
                    :string,
                    "A JSON Pointer [RFC6901] to the associated entity in the request document",
                    default: nil,
                    required: false
                  )
                end
              end
            )
          end
        end,
      Error: JsonApi.single(:ErrorResource)
    }
  end
end

I haven't find a cause of this problem but two possibles problems are:

1.) name of the function is not expected swagger_definitions_common
2.) module doesn't implement :controller behavior

Will investigate further and document it here.

char0n added a commit to char0n/phoenix_swagger that referenced this issue Jan 3, 2020
@char0n char0n linked a pull request Jan 3, 2020 that will close this issue
@char0n
Copy link
Author

char0n commented Jan 3, 2020

@mbuhot I've attached as PR with documentation about extracting common schemas. If you have a time, please have a look at it.

char0n added a commit to char0n/phoenix_swagger that referenced this issue Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants