Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

bug & Error: Response validation failed: failed schema validation #578

Open
xyzdata opened this issue Oct 18, 2018 · 2 comments
Open

bug & Error: Response validation failed: failed schema validation #578

xyzdata opened this issue Oct 18, 2018 · 2 comments

Comments

@xyzdata
Copy link

xyzdata commented Oct 18, 2018

bug & help wanted

Error: Response validation failed: failed schema validation

http://localhost:8080/users?name=xgqfrms

image

image

codes

.yaml

swagger: "2.0"
info:
  version: "1.1.1"
  title: API App
# during dev, should point to your local machine
host: localhost:8080
# basePath prefixes all resource paths
basePath: /
#
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /hello:
    # binds a127 app logic to a route
    x-swagger-router-controller: hello_world
    get:
      description: Returns 'Hello' to the caller
      # used as the method name of the controller
      operationId: hello
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/HelloWorldResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /users:
    # binds a127 app logic to a route
    x-swagger-router-controller: users_api
    get:
      description: Returns 'users' to the caller
      # used as the method name of the controller
      operationId: users
      parameters:
        # - name: name
        # - name: user
        # - user: user & error name
        # - user: name & error name
        # - name: users
        - name: name
          in: query
          description: The name of the person to whom to say users
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/UserApiResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
# complex objects have schema definitions
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
  UserApiResponse:
    required:
        # 202 & respond
      - message
    properties:
      message:
        type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

users_api & controller

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright
 * @description users
 * @augments
 * @example
 *
 */

var util = require('util');
// import { util } from "util";

module.exports = {
    users: users
};

function users(req, res) {
    // req.swagger.params.{parameter_name}
    console.log(`req.swagger.params =`, JSON.stringify(req.swagger.params, null, 4));
    // console.log(`req.swagger.params.users =`, JSON.stringify(req.swagger.params.users, null, 4));
    // console.log(`req.swagger.params.users.value =`, req.swagger.params.users.value);
    // let user_name = req.swagger.params.users.value || "stranger";
    // console.log(`req.swagger.params.user =`, JSON.stringify(req.swagger.params.user, null, 4));
    // console.log(`req.swagger.params.user.value =`, req.swagger.params.user.value);
    // let user_name = req.swagger.params.user.value || "stranger";
    console.log(`req.swagger.params.name =`, JSON.stringify(req.swagger.params.name, null, 4));
    console.log(`req.swagger.params.name.value =`, req.swagger.params.name.value);
    let user_name = req.swagger.params.name.value || "stranger";
    var name = util.format('Hello, %s!', user_name);
    // let users = util.format('Hello, xxx!');
    // res.json({
    //     name
    // });
    res.json({
        "user": name
    });
}
@xyzdata
Copy link
Author

xyzdata commented Oct 18, 2018

what's wrong with this? Why?

help wanted, and THX in advance.

@xgqfrms
Copy link

xgqfrms commented Oct 18, 2018

solution

why ???

image

modify config default.yaml

   validateResponse: false
# swagger configuration file

# values in the swagger hash are system configuration for swagger-node
swagger:

  fittingsDirs: [ api/fittings ]
  defaultPipe: null
  swaggerControllerPipe: swagger_controllers  # defines the standard processing pipe for controllers

  # values defined in the bagpipes key are the bagpipes pipes and fittings definitions
  # (see https://github.com/apigee-127/bagpipes)
  bagpipes:

    _router:
      name: swagger_router
      mockMode: false
      mockControllersDirs: [api/mocks]
      controllersDirs: [api/controllers]

    _swagger_validate:
      name: swagger_validator
      validateResponse: false
      # validateResponse: true

    # pipe for all swagger-node controllers
    swagger_controllers:
      - onError: json_error_handler
      - cors
      - swagger_security
      - _swagger_validate
      - express_compatibility
      - _router

    # pipe to serve swagger (endpoint is in swagger.yaml)
    swagger_raw:
      name: swagger_raw

# any other values in this file are just loaded into the config for application access...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants