Giter Site home page Giter Site logo

Comments (1)

irky avatar irky commented on September 16, 2024

Hello,

I have noticed that only the query parameters marked as required are validated.
For example let's say I have some optional parameter:

paths:
  /things/{namespace}:
    get:
      parameters:
        - $ref: '#/components/parameters/namespace'
        - name: foo
          required: false
          in: query
          schema:
            $ref: '#/components/schemas/foo'

When I do GET /things/test_namespace?bla=something validation passes, but changing required to true makes it fail with code 400 (expected behavior).

In parameters.js file you can see the code:

function buildSchema(parameterObjects) {
    const schema = { query: {}, headers: {}, params: {}, cookies: {} };
    parameterObjects.forEach(parameterObject => {
        const location = parameterObject.in;
        const name = location === "header"
            ? parameterObject.name.toLowerCase()
            : parameterObject.name;
        const parameterSchema = {
            type: "object",
            properties: {
                [name]: parameterObject.schema,
            },
        };
        if (parameterObject.required) {
            parameterSchema.required = [name];
        }
        lodash_1.default.mergeWith(schema[parameterLocationToRequestField(location)], parameterSchema, concatArraysCustomizer);
    });
    return schema;
}

As I checked the if statement is responsible for this behavior. I did a quick test and when you comment out the condition the optional parameter will also be validated. Of course I assume this condition is there for a reason and solution for the problem will be more sophisticated.

        //if (parameterObject.required) {
            parameterSchema.required = [name];
        //}

In my opinion the optional parameters should also be validated, as we don't want to accept anything sent with GET request. At least the other framework for validation express-openapi-validator I used before verified that.

from express-openapi-validate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.