question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

serDes is not working with OpenAPI parameters

See original GitHub issue

Describe the bug When there is described parameter with format in parameters section they are not handled by serDes.

To Reproduce Add format: customOne in requestBody and in parameters and define handler in serDes, only the requestBody will be handled.

Actual behavior Only the requestBody formats are handled by serDes definitions.

Expected behavior All inputs must be handled by serDes definitions.

Examples and context index.js

const OpenApiValidator = require("express-openapi-validator");
const express = require("express");
const http = require("http");

const app = express();

app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: false }));

app.use(
  OpenApiValidator.middleware({
      apiSpec: "./openapi.yaml",
      unknownFormats: ["customOne"],
      serDes: [
        {
          format: "customOne",
          deserialize: (s) => {
            return { customObjectValue: s };
          },
          serialize: (o) => o.toString(),
        }],
    },
  ),
);

app.post("/test/:param1", function(req, res, next) {
  res.json({
    "req.params.param1": req.params.param1,
    "typeof.req.params.param1": typeof req.params.param1, // fail
    "req.params.param2": req.query.param2,
    "typeof.req.params.param2": typeof req.query.param2, // fail
    "req.params.param3": req.body.param3,
    "typeof.req.params.param3": typeof req.body.param3, // success
  });
});

http.createServer(app).listen(3001);
console.log("http://127.0.0.1:3001/");

openapi.yaml

openapi: "3.0.0"
info:
  version: 1.0.0
  title: api-test-gateway

paths:
  /test/{param1}:
    post:
      description: Test serializations
      parameters:
        - name: param1
          in: path
          required: true
          schema:
            type: string
            format: customOne
        - name: param2
          in: query
          required: true
          schema:
            type: string
            format: customOne
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required:
                - param3
              properties:
                param3:
                  type: string
                  format: customOne
      responses:
        "200":
          description: Successful response
          content:
            application/json:
              schema:
                type: object

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
jazzzzcommented, Jun 7, 2021

I found the issue: my parameters are declared under the route, not under the method, like this:

paths:
  /users/{id}:
    parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
          format: mongo-objectid
          pattern: '^[0-9a-fA-F]{24}$'
    get:

The issue also happens for basic types, so it’s not related to serDes.

1reaction
pileroucommented, May 21, 2021

Hi @agavazov and @cdimascio Happy to see serDes feature is used. I try to have a look in the next two weeks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

express-openapi-validator - Bountysource
Describe the bug. When there is described parameter with format in parameters section they are not handled by serDes. To Reproduce
Read more >
Parameter(required = false) not working in swagger open api v3
my problem is with annotation @Parameter that belongs to io.swagger.v3.oas.annotations.Parameter. by default should be required = false, but as ...
Read more >
JSON Schema Serializer and Deserializer
Both the JSON Schema serializer and deserializer can be configured to fail if the payload is not valid for the given schema. This...
Read more >
Parameter Serialization - Swagger
OpenAPI 3.0 supports arrays and objects in operation parameters (path, ... style and explode cover the most common serialization methods, but not all....
Read more >
Working with OpenAPI definitions for HTTP APIs
As another example, while OpenAPI allows users to define an API with multiple security requirements attached to a particular operation, API Gateway does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found