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

Cannot disable defaults middleware #903

Open
coryasilva opened this issue May 7, 2024 · 1 comment
Open

Cannot disable defaults middleware #903

coryasilva opened this issue May 7, 2024 · 1 comment

Comments

@coryasilva
Copy link

Actual:
req.body = { status: 'Draft' }

Expected:
req.body = {}

Steps to Repo:

  1. Configure endpoint with this following doc:
    export const patch = [(req, res, next) => {
      console.log(req.body);
      res.status(204).end();
    }];
    
    patch.apiDoc = {
      "x-express-openapi-disable-defaults-middleware": true,
      requestBody: {
        required: true,
        "application/json": {
          schema: {
            additionalProperties: false,
            properties: {
              status: {
                type: "string",
                default: "Draft",
              },
            },
          },
        },
      },
    };
  2. Console log the request body after making a PATCH command
    curl -X PATCH  --data '{}' -H "content-type: application/json" http://localhost:8080/blah/1

Thoughts:

  • Was this caused by this PR
  • I was trying to opt-out of defaults middleware as described here
@coryasilva
Copy link
Author

coryasilva commented May 7, 2024

Current workaround:

// Body Parser Middleware
app.use(express.json());

// Empty body annotation
app.use((req, _res, next) => {
  if (req.body) {
    /**
     * Annotates request if json body is empty before the schema defaults are
     * applied by the openapi framework's request validator middleware. Cannot
     * opt out of defaults while using the request validation middleware.
     * See issue: https://github.com/kogosoftwarellc/open-api/issues/903
     */
    req.bodyIsEmpty = Object.keys(req.body).length === 0;
  }
  next();
});

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

1 participant