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

Validate path params defined on the path item #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danielfone
Copy link

Since parameters can be defined on both the path item or the operation, we need to validate path params against both objects, like we do with the query params.

Since parameters can be defined on both the path item or the operation, we need to validate path params against both objects, like we do with the query params.
@ota42y ota42y self-requested a review August 10, 2020 09:43
Copy link
Owner

@ota42y ota42y left a comment

Choose a reason for hiding this comment

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

Certainly, we need to check at that place as well.

But when Path Item and Operation have same name path parameters, we should overwrite by the latter.

If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#operationObject

So if the path params is valid in Operation and invalid in Path Item, probably we'll get error 🤔

For example, GET /api/test/test_3 is valid request in this definition, but I think we'll get error.

openapi: 3.0.3
info:
  title: example
  version: '1.0'
paths:
  /api/test/{op}:
    parameters:
      - name: op
        in: path
        required: true
        schema:
          type: string
          enum: 
            - test1
            - test2
    get:
      parameters: 
      - name: op
        in: path
        required: true
        schema:
          type: string
          enum: 
            - test3
      responses:
        default:
          description: sample
          content:
            'application/json':
              schema:
                type: object

So we need merge these parameters and validate.
But this is a potential bug so I can take over and fix it 🙋

@danielfone
Copy link
Author

@ota42y ahh good catch! I'm not likely to be able to look at this for a week or so. If I get to it before you I'm happy to have a go, otherwise all yours. Thanks! 😁

@ota42y
Copy link
Owner

ota42y commented Aug 10, 2020

There is no reason to rush to release a new version, so give it a try! 😄

Since parameters can be defined at either the path item or operation level, operation params are effectively merged into the existing path item params for the given location. Per OpenAPI Specification 3.0.3:

> If a parameter is already defined at the Path Item, the new definition will override it but can never remove it. The list MUST NOT include duplicated parameters. A unique parameter is defined by a combination of a name and location.
@danielfone
Copy link
Author

@ota42y I haven't added any specs for this yet, I wanted to see what you thought of the implementation. I've opted to merge the individual hashes explicitly in the OpenAPIParser::Schemas::Operation class since I think it makes it clearer what's going on.

Alternatively, we could add a public ParameterValidatable#merged_params method and keep the merging all inside the ParameterValidatable concern. Something like:

  # module OpenAPIParser::ParameterValidatable
  def merged_params
    return divided_parameter_hash unless parent&.respond_to?(:merged_params)

    @merged_params ||= Hash.new do |merged_params, location|
      parent_params = parent.merged_params[location] || {}
      merged_params[location] = parent_params.merge(divided_parameter_hash[location])
    end
  end

  def header_parameter_hash
    merged_params['header'] || {}
  end

  # etc…

I think this is a more abstract and less obvious approach though.

What do you think?

@ota42y
Copy link
Owner

ota42y commented Sep 22, 2020

Sorry for late reply... 🙇

I think this is a more abstract and less obvious approach though.
I think so too.

Your approach( merging in the OpenAPIParser::Schemas::Operation ) is very good 👍 👍 👍 👍 👍

  • Performance is good because once merged it will not change after that.
  • Components Object does not have Operation Object in OpenAPI 3 definition so Operation Object is always under the Path Item Object.

@danielfone
Copy link
Author

@ota42y thanks for the feedback. I'll add some specs and ping you for a final review.

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.

2 participants