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

request.query.settings should be object #125

Closed
jy95 opened this issue Nov 13, 2019 · 6 comments
Closed

request.query.settings should be object #125

jy95 opened this issue Nov 13, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@jy95
Copy link
Contributor

jy95 commented Nov 13, 2019

Hello,

Is serialized objects not supported or am I doing something wrong?
I have an endpoint defined like that ( I deliberately simplified this ) :

  /api/someGet:
    get:
      summary: "Retrieve something"
      parameters:
        - in: query
          style: form
          name: settings
          explode: true
          schema:
            allOf:
              - type: object
                properties:
                  onlyValidated:
                    type: boolean
                    default: true
                  onlySelected:
                    type: array
                    default: []
                    uniqueItems: true
                    items:
                      type: integer
                      minimum: 0
                      example: 42
      responses:
        '200':
          description: "An array of items"
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/someItem"
                uniqueItems: true

I tested two approaches that have failed with Postman ( that imported the api definition ) :

{{baseUrl}}/api/someGet?settings={"onlyValidated":true}

{
    "message": "request.query.settings should be object",
    "errors": [
        {
            "path": ".query.settings",
            "message": "should be object",
            "errorCode": "type.openapi.validation"
        }
    ]
}

{{baseUrl}}/api/someGet?onlyValidated=true

{
    "message": "Unknown query parameter onlyValidated",
    "errors": [
        {
            "path": ".query.onlyValidated",
            "message": "Unknown query parameter onlyValidated"
        }
    ]
}

FYI, here is my express server implementation :

const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');

const routes = require('./routes/index');

// miscellaneous passport things
const passport = require('passport');


// OpenAPI V3 validation middleware
const OpenApiValidator = require('express-openapi-validator').OpenApiValidator;
const spec = path.join(__dirname, 'api.yml');

// Initialize passport ( Passport is a singleton )
require('./config/passport');

const app = express();

// middleware
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

// API validation before routes and password.js
// Install the OpenApiValidator on your express app
new OpenApiValidator({
    apiSpec: spec,
    validateRequests: true,
    validateResponses: false,
    // securityHandlers: {
    //   ApiKeyAuth: (req, scopes, schema) => true,
    // },
}).install(app);

// Passport Js must have that
app.use(passport.initialize());

// routes
app.use('/', routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    let err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
// no stacktraces leaked to user unless in development environment
app.use(function(err, req, res, next) {

    // if error thrown by validation, give everything else depending of the environment
    res.status(err.status || 500).json({
        message: err.message,
        errors: err.hasOwnProperty("errors")
            ? err.errors
            : (app.get('env') === 'development') ? [err] : []
    });

});


module.exports = app;
@cdimascio
Copy link
Owner

@jy95 serialized objects are not yet supported. this is definitely something we will plan to support. hopefully soon. this weekend i'll at least have a look at what is involved. thanks for raising this

@jy95
Copy link
Contributor Author

jy95 commented Nov 27, 2019

any possible workaround? ( Until this is implemented)

@cdimascio
Copy link
Owner

If you upgrade to the latest version, you can use the ignorePaths option to skip validation on any routes that have this requirement. It's certainly not a solution, but it should allow you to work around the issue while waiting on the implementation

@comino have you been able to make any progress on the impl?

cdimascio added a commit that referenced this issue Dec 3, 2019
WIP: create test for request.query.settings should be object #125
@cdimascio
Copy link
Owner

@jy95 please give v3.0.2 a try.
it should improve the query param serialization handling. please let me know how it goes

@jy95
Copy link
Contributor Author

jy95 commented Dec 3, 2019

Thanks @cdimascio : I will test it when I have time this week ( I have problems with multipart/form-data queries to deal with first )

@jy95
Copy link
Contributor Author

jy95 commented Dec 6, 2019

It works like a charm : I can access object properties as expected ( req.query.settings )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants