Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BUG] oas tool 3.0 fail to treat optional field in schema as option.  #356

@cll023

Description

@cll023

Describe the bug

When we have a schema that defines a field of object type as optional, and if in the request body we don't include that optional field, oas tool complains.

To Reproduce

Steps to reproduce the behavior:

  1. the schema of object definition as the following:
  schemas:
    TestDocument:
      type: object
      required:
      - version
      - id
      properties:
        version:
          description: Schema version
          type: string
        id:
          description: Unique ID
          type: string
          example: 1234567
        owner:
          $ref: '#/components/schemas/Owner'
    Owner:
      type: object
      required:
        - id
      properties:
        id:
          type: string
          format: uri
        name:
          type: string
          minLength: 1
          maxLength: 256
  1. The following is the configuration to initiate the api server
const express = require("express");
const { initialize } = require('@oas-tools/core');


const serverPort = 8000;
const app = express();
app.use(express.json({limit: '50mb'}));
const config = {
    middleware: {
        validator: {
            strict: true,
            requestValidation: true,
            responseValidation: true,
        },
    }
}


initialize(app, config).then(() => {
    http.createServer(app).listen(serverPort, () => {
    console.log("\nApp running at http://localhost:" + serverPort);
    console.log("________________________________________________________________");
    if (config.middleware.swagger?.disable !== false) {
        console.log('API docs (Swagger UI) available on http://localhost:' + serverPort + '/docs');
        console.log("________________________________________________________________");
    }
    });
});
  1. Send the following in a request body
    {
    "version": "v1",
    "id": "12"
    }

Expected behavior

This should pass the schema validation.
Instead, the API sever return 404 with the following error
{
"error": "RequestValidationError: Request body does not match the schema specified in the OAS Document:\n- Validation failed at #/properties/owner/required > must have required property 'id'"
}

Screenshots

Additional context

this is for oas-tool version "3.0.3

The problem is with parseBody function that it would set "owner" value as {} . it results in the schema validation. The "owner" should be omitted in this case.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions