diff options
| author | lain <lain@soykaf.club> | 2020-04-24 16:10:43 +0000 | 
|---|---|---|
| committer | lain <lain@soykaf.club> | 2020-04-24 16:10:43 +0000 | 
| commit | d862c7156a1edcdc89425143901f140bc0b1f5eb (patch) | |
| tree | e5f3e28956e01930ff4c21ac982733ef718645d5 /lib | |
| parent | 2430b9bf90d6fde71885f5f0f8be767526be6208 (diff) | |
| parent | f362836742aabd5b60b92c1296f2bbb6d83a3d59 (diff) | |
| download | pleroma-d862c7156a1edcdc89425143901f140bc0b1f5eb.tar.gz pleroma-d862c7156a1edcdc89425143901f140bc0b1f5eb.zip | |
Merge branch 'openapi-improvements' into 'develop'
Support validation for inline OpenAPI schema and automatic tests for examples
Closes #1696
See merge request pleroma/pleroma!2421
Diffstat (limited to 'lib')
9 files changed, 121 insertions, 160 deletions
| diff --git a/lib/pleroma/web/api_spec/operations/app_operation.ex b/lib/pleroma/web/api_spec/operations/app_operation.ex index 26d8dbd42..035ef2470 100644 --- a/lib/pleroma/web/api_spec/operations/app_operation.ex +++ b/lib/pleroma/web/api_spec/operations/app_operation.ex @@ -6,8 +6,6 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do    alias OpenApiSpex.Operation    alias OpenApiSpex.Schema    alias Pleroma.Web.ApiSpec.Helpers -  alias Pleroma.Web.ApiSpec.Schemas.AppCreateRequest -  alias Pleroma.Web.ApiSpec.Schemas.AppCreateResponse    @spec open_api_operation(atom) :: Operation.t()    def open_api_operation(action) do @@ -22,9 +20,9 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do        summary: "Create an application",        description: "Create a new application to obtain OAuth2 credentials",        operationId: "AppController.create", -      requestBody: Helpers.request_body("Parameters", AppCreateRequest, required: true), +      requestBody: Helpers.request_body("Parameters", create_request(), required: true),        responses: %{ -        200 => Operation.response("App", "application/json", AppCreateResponse), +        200 => Operation.response("App", "application/json", create_response()),          422 =>            Operation.response(              "Unprocessable Entity", @@ -93,4 +91,58 @@ defmodule Pleroma.Web.ApiSpec.AppOperation do        }      }    end + +  defp create_request do +    %Schema{ +      title: "AppCreateRequest", +      description: "POST body for creating an app", +      type: :object, +      properties: %{ +        client_name: %Schema{type: :string, description: "A name for your application."}, +        redirect_uris: %Schema{ +          type: :string, +          description: +            "Where the user should be redirected after authorization. To display the authorization code to the user instead of redirecting to a web page, use `urn:ietf:wg:oauth:2.0:oob` in this parameter." +        }, +        scopes: %Schema{ +          type: :string, +          description: "Space separated list of scopes", +          default: "read" +        }, +        website: %Schema{type: :string, description: "A URL to the homepage of your app"} +      }, +      required: [:client_name, :redirect_uris], +      example: %{ +        "client_name" => "My App", +        "redirect_uris" => "https://myapp.com/auth/callback", +        "website" => "https://myapp.com/" +      } +    } +  end + +  defp create_response do +    %Schema{ +      title: "AppCreateResponse", +      description: "Response schema for an app", +      type: :object, +      properties: %{ +        id: %Schema{type: :string}, +        name: %Schema{type: :string}, +        client_id: %Schema{type: :string}, +        client_secret: %Schema{type: :string}, +        redirect_uri: %Schema{type: :string}, +        vapid_key: %Schema{type: :string}, +        website: %Schema{type: :string, nullable: true} +      }, +      example: %{ +        "id" => "123", +        "name" => "My App", +        "client_id" => "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM", +        "client_secret" => "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw", +        "vapid_key" => +          "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=", +        "website" => "https://myapp.com/" +      } +    } +  end  end diff --git a/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex b/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex index cf2215823..a117fe460 100644 --- a/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex +++ b/lib/pleroma/web/api_spec/operations/custom_emoji_operation.ex @@ -4,7 +4,8 @@  defmodule Pleroma.Web.ApiSpec.CustomEmojiOperation do    alias OpenApiSpex.Operation -  alias Pleroma.Web.ApiSpec.Schemas.CustomEmojisResponse +  alias OpenApiSpex.Schema +  alias Pleroma.Web.ApiSpec.Schemas.CustomEmoji    def open_api_operation(action) do      operation = String.to_existing_atom("#{action}_operation") @@ -18,8 +19,43 @@ defmodule Pleroma.Web.ApiSpec.CustomEmojiOperation do        description: "Returns custom emojis that are available on the server.",        operationId: "CustomEmojiController.index",        responses: %{ -        200 => Operation.response("Custom Emojis", "application/json", CustomEmojisResponse) +        200 => Operation.response("Custom Emojis", "application/json", custom_emojis_resposnse())        }      }    end + +  defp custom_emojis_resposnse do +    %Schema{ +      title: "CustomEmojisResponse", +      description: "Response schema for custom emojis", +      type: :array, +      items: CustomEmoji, +      example: [ +        %{ +          "category" => "Fun", +          "shortcode" => "blank", +          "static_url" => "https://lain.com/emoji/blank.png", +          "tags" => ["Fun"], +          "url" => "https://lain.com/emoji/blank.png", +          "visible_in_picker" => false +        }, +        %{ +          "category" => "Gif,Fun", +          "shortcode" => "firefox", +          "static_url" => "https://lain.com/emoji/Firefox.gif", +          "tags" => ["Gif", "Fun"], +          "url" => "https://lain.com/emoji/Firefox.gif", +          "visible_in_picker" => true +        }, +        %{ +          "category" => "pack:mixed", +          "shortcode" => "sadcat", +          "static_url" => "https://lain.com/emoji/mixed/sadcat.png", +          "tags" => ["pack:mixed"], +          "url" => "https://lain.com/emoji/mixed/sadcat.png", +          "visible_in_picker" => true +        } +      ] +    } +  end  end diff --git a/lib/pleroma/web/api_spec/operations/domain_block_operation.ex b/lib/pleroma/web/api_spec/operations/domain_block_operation.ex index dd14837c3..3b7f51ceb 100644 --- a/lib/pleroma/web/api_spec/operations/domain_block_operation.ex +++ b/lib/pleroma/web/api_spec/operations/domain_block_operation.ex @@ -6,8 +6,6 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do    alias OpenApiSpex.Operation    alias OpenApiSpex.Schema    alias Pleroma.Web.ApiSpec.Helpers -  alias Pleroma.Web.ApiSpec.Schemas.DomainBlockRequest -  alias Pleroma.Web.ApiSpec.Schemas.DomainBlocksResponse    def open_api_operation(action) do      operation = String.to_existing_atom("#{action}_operation") @@ -22,7 +20,13 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do        security: [%{"oAuth" => ["follow", "read:blocks"]}],        operationId: "DomainBlockController.index",        responses: %{ -        200 => Operation.response("Domain blocks", "application/json", DomainBlocksResponse) +        200 => +          Operation.response("Domain blocks", "application/json", %Schema{ +            description: "Response schema for domain blocks", +            type: :array, +            items: %Schema{type: :string}, +            example: ["google.com", "facebook.com"] +          })        }      }    end @@ -40,7 +44,7 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do        - prevent following new users from it (but does not remove existing follows)        """,        operationId: "DomainBlockController.create", -      requestBody: Helpers.request_body("Parameters", DomainBlockRequest, required: true), +      requestBody: domain_block_request(),        security: [%{"oAuth" => ["follow", "write:blocks"]}],        responses: %{          200 => Operation.response("Empty object", "application/json", %Schema{type: :object}) @@ -54,11 +58,28 @@ defmodule Pleroma.Web.ApiSpec.DomainBlockOperation do        summary: "Unblock a domain",        description: "Remove a domain block, if it exists in the user's array of blocked domains.",        operationId: "DomainBlockController.delete", -      requestBody: Helpers.request_body("Parameters", DomainBlockRequest, required: true), +      requestBody: domain_block_request(),        security: [%{"oAuth" => ["follow", "write:blocks"]}],        responses: %{          200 => Operation.response("Empty object", "application/json", %Schema{type: :object})        }      }    end + +  defp domain_block_request do +    Helpers.request_body( +      "Parameters", +      %Schema{ +        type: :object, +        properties: %{ +          domain: %Schema{type: :string} +        }, +        required: [:domain] +      }, +      required: true, +      example: %{ +        "domain" => "facebook.com" +      } +    ) +  end  end diff --git a/lib/pleroma/web/api_spec/schemas/app_create_request.ex b/lib/pleroma/web/api_spec/schemas/app_create_request.ex deleted file mode 100644 index 8a83abef3..000000000 --- a/lib/pleroma/web/api_spec/schemas/app_create_request.ex +++ /dev/null @@ -1,33 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.AppCreateRequest do -  alias OpenApiSpex.Schema -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "AppCreateRequest", -    description: "POST body for creating an app", -    type: :object, -    properties: %{ -      client_name: %Schema{type: :string, description: "A name for your application."}, -      redirect_uris: %Schema{ -        type: :string, -        description: -          "Where the user should be redirected after authorization. To display the authorization code to the user instead of redirecting to a web page, use `urn:ietf:wg:oauth:2.0:oob` in this parameter." -      }, -      scopes: %Schema{ -        type: :string, -        description: "Space separated list of scopes. If none is provided, defaults to `read`." -      }, -      website: %Schema{type: :string, description: "A URL to the homepage of your app"} -    }, -    required: [:client_name, :redirect_uris], -    example: %{ -      "client_name" => "My App", -      "redirect_uris" => "https://myapp.com/auth/callback", -      "website" => "https://myapp.com/" -    } -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/app_create_response.ex b/lib/pleroma/web/api_spec/schemas/app_create_response.ex deleted file mode 100644 index f290fb031..000000000 --- a/lib/pleroma/web/api_spec/schemas/app_create_response.ex +++ /dev/null @@ -1,33 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.AppCreateResponse do -  alias OpenApiSpex.Schema - -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "AppCreateResponse", -    description: "Response schema for an app", -    type: :object, -    properties: %{ -      id: %Schema{type: :string}, -      name: %Schema{type: :string}, -      client_id: %Schema{type: :string}, -      client_secret: %Schema{type: :string}, -      redirect_uri: %Schema{type: :string}, -      vapid_key: %Schema{type: :string}, -      website: %Schema{type: :string, nullable: true} -    }, -    example: %{ -      "id" => "123", -      "name" => "My App", -      "client_id" => "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM", -      "client_secret" => "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw", -      "vapid_key" => -        "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M=", -      "website" => "https://myapp.com/" -    } -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex b/lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex deleted file mode 100644 index 01582a63d..000000000 --- a/lib/pleroma/web/api_spec/schemas/custom_emojis_response.ex +++ /dev/null @@ -1,42 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.CustomEmojisResponse do -  alias Pleroma.Web.ApiSpec.Schemas.CustomEmoji - -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "CustomEmojisResponse", -    description: "Response schema for custom emojis", -    type: :array, -    items: CustomEmoji, -    example: [ -      %{ -        "category" => "Fun", -        "shortcode" => "blank", -        "static_url" => "https://lain.com/emoji/blank.png", -        "tags" => ["Fun"], -        "url" => "https://lain.com/emoji/blank.png", -        "visible_in_picker" => true -      }, -      %{ -        "category" => "Gif,Fun", -        "shortcode" => "firefox", -        "static_url" => "https://lain.com/emoji/Firefox.gif", -        "tags" => ["Gif", "Fun"], -        "url" => "https://lain.com/emoji/Firefox.gif", -        "visible_in_picker" => true -      }, -      %{ -        "category" => "pack:mixed", -        "shortcode" => "sadcat", -        "static_url" => "https://lain.com/emoji/mixed/sadcat.png", -        "tags" => ["pack:mixed"], -        "url" => "https://lain.com/emoji/mixed/sadcat.png", -        "visible_in_picker" => true -      } -    ] -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/domain_block_request.ex b/lib/pleroma/web/api_spec/schemas/domain_block_request.ex deleted file mode 100644 index ee9238361..000000000 --- a/lib/pleroma/web/api_spec/schemas/domain_block_request.ex +++ /dev/null @@ -1,20 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.DomainBlockRequest do -  alias OpenApiSpex.Schema -  require OpenApiSpex - -  OpenApiSpex.schema(%{ -    title: "DomainBlockRequest", -    type: :object, -    properties: %{ -      domain: %Schema{type: :string} -    }, -    required: [:domain], -    example: %{ -      "domain" => "facebook.com" -    } -  }) -end diff --git a/lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex b/lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex deleted file mode 100644 index d895aca4e..000000000 --- a/lib/pleroma/web/api_spec/schemas/domain_blocks_response.ex +++ /dev/null @@ -1,16 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ApiSpec.Schemas.DomainBlocksResponse do -  require OpenApiSpex -  alias OpenApiSpex.Schema - -  OpenApiSpex.schema(%{ -    title: "DomainBlocksResponse", -    description: "Response schema for domain blocks", -    type: :array, -    items: %Schema{type: :string}, -    example: ["google.com", "facebook.com"] -  }) -end diff --git a/lib/pleroma/web/oauth/scopes.ex b/lib/pleroma/web/oauth/scopes.ex index 1023f16d4..6f06f1431 100644 --- a/lib/pleroma/web/oauth/scopes.ex +++ b/lib/pleroma/web/oauth/scopes.ex @@ -17,12 +17,8 @@ defmodule Pleroma.Web.OAuth.Scopes do    """    @spec fetch_scopes(map() | struct(), list()) :: list() -  def fetch_scopes(%Pleroma.Web.ApiSpec.Schemas.AppCreateRequest{scopes: scopes}, default) do -    parse_scopes(scopes, default) -  end -    def fetch_scopes(params, default) do -    parse_scopes(params["scope"] || params["scopes"], default) +    parse_scopes(params["scope"] || params["scopes"] || params[:scopes], default)    end    def parse_scopes(scopes, _default) when is_list(scopes) do | 
