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

Response Validation Issue - "Cannot read property 'then' of undefined in modded.mung" #61

Closed
spencerbn opened this issue Oct 7, 2019 · 6 comments

Comments

@spencerbn
Copy link
Contributor

Hi,

It looks like response validation was just added a few days ago, so this could be a known issue or not. So far I really appreciate how straight-forward and friendly this library is. Thank you!

Now to the issue:

I have a simple example app using OpenApiValidator to validate requests and responses.

I am sending the request

http://localhost:3000/v1/progress?type=test

When I have the validateResponses flag set to false, my response is returned correctly (although not validated of course). However when I set validateResponses to true I get:

TypeError: Cannot read property 'then' of undefined
    at ServerResponse.json_async_hook (node_modules/express-openapi-validator/dist/middlewares/modded.express.mung.js:73:21)
    at app.ts:24:7
    at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
    at next (node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
    at node_modules/express/lib/router/index.js:281:22
    at Function.process_params (node_modules/express/lib/router/index.js:335:12)
    at next (node_modules/express/lib/router/index.js:275:10)
    at node_modules/express-openapi-validator/dist/middlewares/modded.express.mung.js:94:17

Line 24 in my app.ts is pretty straightforward:

23 app.get('/v1/progress', function(req, res, next) {
24   res.json({id: 1, name: 'test'});
25 });

So I am trying to figure out if I mis-configured the project or if its existing issue.
I would really appreciate any feedback on how I can get this to work!

app.ts

const app = express();
app.use(express.json());
new OpenApiValidator({
  apiSpec: './workflow-tracker-openapi.yml',
  validateRequests: true,
  validateResponses: true,
}).install(app);

app.get('/v1/progress', function(req, res, next) {
  res.json({id: 1, name: 'test'});
});

const server = app.listen(3000);

yml spec

info:
  title: Example Service API
  version: '1.0'
servers:
  - url: http://localhost:3000/v1

paths:
  /progress:
    get:
      description: Get the progress of an event
      parameters:
        - name: type
          in: query
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Progress'
          
components:
  schemas:
    Progress:
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
      required:
        - id
        - name

I really appreciate that you've made this library available and will keep digging through the source to see if I can fix it myself, but if you have any idea what might be wrong in the meantime, please let me know.

Thank you!

@cdimascio
Copy link
Owner

@SpencerLawrenceBrown thanks for the issue.
which version of express-openapi-validator are you using?

@spencerbn
Copy link
Contributor Author

2.3.0

@spencerbn
Copy link
Contributor Author

it seems to be related to how

try {
    fn(json, req, res)
} catch ...

is compiled to js. When its compiled it becomes:

 try {
                fn(json, req, res)
                    .then(json => {
                    if (res.headersSent)
                        return;
                    // If null, then 204 No Content
                    if (json === null)
                        return res.status(204).end();
                    // If munged scalar value, then text/plain
                    if (json !== originalJson && isScalar(json)) {
                        res.set('content-type', 'text/plain');
                        return res.send(String(json));
                    }
                    return original.call(this, json);
                })
                    .catch(e => mung.onError(e, req, res, next));
            }

@spencerbn
Copy link
Contributor Author

^^ in modded.express.mung

@spencerbn
Copy link
Contributor Author

I just downgraded to 2.1.5 and it works fine. Looks like a change between 2.1.5 and 2.3.0

cdimascio pushed a commit that referenced this issue Oct 7, 2019
cdimascio added a commit that referenced this issue Oct 7, 2019
Response Validation Issue - "Cannot read property 'then' of undefined in modded.mung" #61
@cdimascio
Copy link
Owner

Thanks @SpencerLawrenceBrown. I spotted the issue. It should be resolved in v2.3.1.

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

No branches or pull requests

2 participants