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

wip: cast parameters with json content-type #445

Merged
merged 1 commit into from
Jun 10, 2022

Conversation

albertored
Copy link
Contributor

Hello, I'm trying to fix #349

At the moment I'm am able to correctly cast parameters that are encoded as JSON but I have some questions to understand how you think is better to proceed:

  • should I create a new error type for taking into account situations in which the parameter can not be decoded? (atm I'm using :invalid_format)
  • I like your suggestion @mbuhot of registering parsers with something like OpenApiSpex.add_content_parser but I think we should allow it specifically for this situation, thus something like OpenApiSpex.add_parameter_content_parser, in other circumstances we already have Plug.Parsers. What do you think?
  • I think we can have some default parameter_content_parsers like the JSON one
  • To support parameter_content_parsers registration I only need to pass the information to cast_parameters.ex, where should we store this info? Should we create a new field on the root OpenApi struct?

Tagging also @moxley since it was involved in the original issue, sorry for the noise :-)

@albertored
Copy link
Contributor Author

Any suggestion on the open points?

@mbuhot
Copy link
Collaborator

mbuhot commented Jun 7, 2022

should I create a new error type for taking into account situations in which the parameter can not be decoded?

:invalid_format seems like a good choice. So long as the message from OpenApiSpex.Cast.Error.message makes sense, eg: "Invalid format. Expected application/json"

registering parsers with something like OpenApiSpex.add_content_parser but I think we should allow it specifically for this situation, thus something like OpenApiSpex.add_parameter_content_parser, in other circumstances we already have Plug.Parsers. What do you think?

Yes that makes sense. If OpenApiSpex is only going to use the supplied parser for handling Parameters (not body), then making that explicit in the function name will help.

I think we can have some default parameter_content_parsers like the JSON one

Yes it's helpful to have one included in the library that can be used as a reference.

To support parameter_content_parsers registration I only need to pass the information to cast_parameters.ex, where should we store this info? Should we create a new field on the root OpenApi struct?

Can we use the extensions facility? For schemas we have x-struct and x-validate to store the names of modules.
The OpenApi struct has the extensions field which could store some data under an "x-parameter-content-parsers" key?

@albertored albertored force-pushed the parameters-content-type branch 3 times, most recently from a3778db to 3448db8 Compare June 9, 2022 14:51
@albertored albertored force-pushed the parameters-content-type branch from 3448db8 to a630e99 Compare June 9, 2022 14:56
@albertored
Copy link
Contributor Author

@mbuhot done

Now the OpenApiSpex module exposes the following function

@spec add_parameter_content_parser(
          OpenApi.t(),
          content_type | [content_type],
          parser :: module()
        ) :: OpenApi.t()
        when content_type: String.t() | Regex.t()

A parser can be defined for specific content types (strings that should match exactly) or for regular expressions matching multiple content types.

Exact matches are checked first, then regex ones and finally default ones.

The only default is

%{
  ~r/^application\/.*json.*$/ => OpenApiSpex.OpenApi.json_encoder()
}

@mbuhot
Copy link
Collaborator

mbuhot commented Jun 10, 2022

Thanks @albertored !

@mbuhot mbuhot merged commit 9127f60 into open-api-spex:master Jun 10, 2022
@albertored albertored deleted the parameters-content-type branch June 16, 2022 12:47
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.

Support for content and deserialized path parameters
2 participants