Coder Social home page Coder Social logo

Comments (7)

juliusza avatar juliusza commented on August 17, 2024 2

Thanks, you're awesome. Looks like I'll be able to replace swaggerize-express lib with this one after all.

from open-api.

juliusza avatar juliusza commented on August 17, 2024

world.js

module.exports = {
    put: function (req, res, next) {
        console.log(req.body);
        res.json({ok: "ok"});
    }
};

main.js

const apiDoc = {
    swagger: '2.0',
    info: {
        title: 'A getting started API.',
        version: '1.0.0'
    },
    paths: {
        "/world": {
            "put": {
                parameters: [
                    {
                        in: 'body',
                        name: 'body',
                        required: true,
                        schema: {
                            type: "object",
                            properties: {
                                filtered: {
                                    description: "test",
                                    type: "boolean"
                                }
                            },
                            required: ["filtered"]
                        }
                    }
                ],
                responses: {
                    200: {
                        description: "ok",
                    },
                    default: {
                        description: "error",
                    }
                }
            }
        }
    }
};

const bodyParser = require("body-parser");
const express = require("express");
const openapi = require("express-openapi");
const http = require("http");
const app = express();

app.use(
    bodyParser.json(),
    bodyParser.urlencoded({ extended: false })
);

openapi.initialize({
    app,
    apiDoc: apiDoc,
    paths: "./api-v1/paths"
});

app.listen(3200);

setTimeout(function () {
    // Required property filtered is not set
    const postData = JSON.stringify({test: "test"});

    const options = {
        hostname: "127.0.0.1",
        port: 3200,
        path: "/world",
        method: "PUT",
        headers: {
            "Content-Type": "application/json",
            "Content-Length": Buffer.byteLength(postData)
        }
    };

    console.log("Making request");
    const req = http.request(options, res => {
        console.log("Got response from " + options.path, res.statusCode);
        req.on("data", data => console.log(data));
        req.on("end", data => console.log("END"));
    });

    req.on("error", err => console.log(err));
    req.write(postData);
    req.end();

}, 1000);

Got output:

Making request
{ test: 'test' }
Got response from /world 200

Expected output:

Making request
Got response from /world 400

from open-api.

jsdevel avatar jsdevel commented on August 17, 2024

The schema seems right. Have you looked at the sample projects? Here's a spec from the basic-usage project that seems to do what you want: https://github.com/kogosoftwarellc/express-openapi/blob/master/test/sample-projects/basic-usage/spec.js#L77

from open-api.

juliusza avatar juliusza commented on August 17, 2024

I was breaking my head for hours as to why this doesn't work.

This unreleased change fixes the issue: 471f9a3#diff-168726dbe96b3ce427e7fedce31bb0bc

how about making a release? @jsdevel

from open-api.

jsdevel avatar jsdevel commented on August 17, 2024

@juliusza done. published @alrik 's PR as v1.5.0

from open-api.

jsdevel avatar jsdevel commented on August 17, 2024

@juliusza if the latest version works for you, would you mind closing this? Let us know if you still have problems. I see why it didn't work for you now, as defining paths in the global apiDoc wasn't added until @alrik 's PR. Should be good to go now.

from open-api.

jsdevel avatar jsdevel commented on August 17, 2024

You're welcome! Happy to have you! Don't hesitate to report issues here.

from open-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.