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

Validates OpenAPI Specification on upload and redesigns spec entry #45

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

floriank
Copy link
Collaborator

This will require users to enter specs a bit differently, since there is
a couple of problems with the way we're handling this at the moment:

Before

// Request body as JSON:

{ "spec": { "openapi": "3.0.0" } } // etc.

This will break under certain circumstances as param mapping will not
accept this in Phoenix (since server lists can be maps with multiple
keys). It also omits the whole "we know whether this is YAML or JSON at
runtime".

After

// Request body as JSON:

{ "json": " {\"openapi\": \"3.0.0\"}" } // etc.

This allows for knowing what spec we're parsing and will also open up
other entrypoints more easily (e.g. fetching from a URL, uploading
directly etc.)

Bonus

This now also validates the OpenAPI specification given using the https://github.com/mimicry-tech/openapi-validator.

Fixes #15

This will require users to enter specs a bit differently, since there is
a couple of problems with the way we're handling this at the moment:

Before
------
```
// Request body as JSON:

{ "spec": { "openapi": "3.0.0" } } // etc.
```

This will break under certain circumstances as param mapping will not
accept this in Phoenix (since server lists can be maps with multiple
keys). It also omits the whole "we know whether this is YAML or JSON at
runtime".

After
-----
```
// Request body as JSON:

{ "json": " {\"openapi\": \"3.0.0\"}" } // etc.
```

This allows for knowing what spec we're parsing and will also open up
other entrypoints more easily (e.g. fetching from a URL, uploading
directly etc.)
...or: how I figured out my editor at home is misconfigured
@coveralls
Copy link

coveralls commented Sep 13, 2021

Pull Request Test Coverage Report for Build e734412cf822148dbb96ea1d3b2287972fedebfd-PR-45

  • 17 of 22 (77.27%) changed or added relevant lines in 5 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.3%) to 68.75%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/mimicry_api/controllers/proxy_controller.ex 2 3 66.67%
test/support/fixtures.ex 2 3 66.67%
lib/mimicry/open_api/parser.ex 2 5 40.0%
Files with Coverage Reduction New Missed Lines %
lib/mimicry_api/controllers/server_controller.ex 1 77.78%
Totals Coverage Status
Change from base Build f386162207efc5ae304d7532afccf6e9dfb2dc9f: -0.3%
Covered Lines: 154
Relevant Lines: 224

💛 - Coveralls

@floriank floriank changed the title Validates OpenAPI Specication on uploadredesigns spec entry Validates OpenAPI Specification on upload and redesigns spec entry Sep 14, 2021
Copy link
Collaborator

@asaaki asaaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to do the job.
Just a few tiny questions, but mostly around code specs, so nothing blocking.

@doc """
parses a given string to a map
"""
@spec parse_to_map(any(), :yaml | :json) :: :error | any()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the doc says string, shouldn't it be also in the spec?

Suggested change
@spec parse_to_map(any(), :yaml | :json) :: :error | any()
@spec parse_to_map(String.t(), :yaml | :json) :: :error | any()

Comment on lines -23 to +25
@spec parse(String.t(), :yaml | :json) :: Specification.t()
@spec parse(term(), :yaml | :json) :: Specification.t()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this spec change?
Also shouldn't parse/2 and parse_to_map/2 have the same definition for the inputs?

"""
@spec parse(String.t(), :yaml | :json) :: Specification.t()
@spec parse(term(), :yaml | :json) :: Specification.t()
def parse(str, atom) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function could now be shortened to something like this:

def parse(str, atom) do
  str
  |> parse_to_map(atom)
  |> case do
    {:ok, decoded} ->
      decoded |> build_specification()
    {:error, _err} ->
      Specification.unsupported()
  end
end

Comment on lines -41 to +61
@spec build_specification(map()) :: Specification.t()
@spec build_specification(any()) :: Specification.t()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above about spec changes.

@@ -69,6 +69,7 @@ defmodule MimicryApi.ProxyControllerTest do
end

@tag server: "simple.yaml"
@tag :focus
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a local test leftover?

Suggested change
@tag :focus

asaaki referenced this pull request in asaaki/mimicry Oct 16, 2021
Change in accordance with #45.
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 this pull request may close these issues.

Validate OpenAPI Specification
3 participants