GithubHelp home page GithubHelp logo

nijens / openapi-bundle Goto Github PK

View Code? Open in Web Editor NEW
15.0 15.0 4.0 771 KB

Helps you create a REST API from your OpenAPI specification.

License: MIT License

PHP 99.27% Makefile 0.73%
openapi openapi3 php symfony symfony-bundle

openapi-bundle's People

Contributors

beau-ottens avatar hansgoed avatar kiettran avatar niels-nijens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

openapi-bundle's Issues

Improve validation for requests without required JSON properties

At the moment, request validation fails when a request without request body is sent to a route, but a requestBody is defined in the loaded OpenAPI document. Even though no JSON properties are required or the requestBody is defined as required.

Let's assume the following OpenAPI document:

openapi: 3.1.0

info:
  title: OpenAPI document with operation that should allow no response body.
  version: 0.1.0

paths:
  /pets:
    post:
      x-openapi-bundle:
        controller: 'Nijens\OpenapiBundle\Tests\Functional\App\Controller\CreatePetController'
        deserializationObject: 'Nijens\OpenapiBundle\Tests\Functional\App\Model\CreatePet'
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
        required: false
      responses:
        '201':
          description: Successfully added a new pet to the store.

With this OpenAPI document, it should be possible to send a request without an empty JSON object. Though, at the moment the bundle will return 400 Bad Request when sending a request without JSON request body. This should be improved.

Feedback

In openapi-bundle/src/EventListener/JsonRequestBodyValidationSubscriber.php:

  • Some exceptions are thrown, however the PHPDoc does not contain @throws tags
  • openapi_resource and openapi_json_request_validation_pointer are re-used, maybe create constants for them?

In openapi-bundle/src/Json/JsonPointer.php:

  • Class documentation is a little weak

Request body validation returns unclear allOf constraint violation in Bad request response

Upon sending a request with an invalid JSON request body, the response returns a allOf constraint violation in the list of violations when allOf is used in the request body JSON schema of the operation. This constraint violation is unclear in the context of the response as the actual constraint violation is above it.

Assume the following request body JSON schema:

requestBody:
  content:
    application/json:
      schema:
        allOf:
          - $ref: '#/components/schemas/SomeObject'
          - type: object
            properties:
              someProperty:
                type: string
            required:
              - someProperty

When sending a request to the operation without someProperty property inside the request body, the following error response is returned:

{
  "type": "about:blank",
  "title": "The request body contains errors.",
  "status": 400,
  "detail": "Validation of JSON request body failed.",
  "violations": [
    {
      "constraint": "required",
      "message": "The property someProperty is required",
      "property": "someProperty"
    },
    {
      "constraint": "allOf",
      "message": "Failed to match all schemas",
      "property": ""
    }
  ]
}

Here the allOf constraint violation is unclear and redundant for the API consumer. I'd expect the response to be the following:

{
  "type": "about:blank",
  "title": "The request body contains errors.",
  "status": 400,
  "detail": "Validation of JSON request body failed.",
  "violations": [
    {
      "constraint": "required",
      "message": "The property someProperty is required",
      "property": "someProperty"
    }
  ]
}

Missing source files after composer install

After installing the openapi bundle, the source files could not be found. The src directory is empty. This happens on different composer versions (1.9.x, 1.10.x and 2.x) and on different projects. From existing to new symfony 5 projects.

Page load slowdowns on route-heavy projects.

I found that the page loads of my project increased with more than a second after installing the openapi-bundle.

Using the profiler I found that this was the JsonRequestBodyValidationSubscriber loading the routes collection. The routes collection of this project contains 1100 routes.

(return) type declaration consistency

Most class methods have strict typehints for arguments and return types added, but not all methods have them set at the moment. This needs to be double-checked.

Validation of allowed request content types according to the loaded OpenAPI document

Even though the bundle currently only supports the validation of a JSON request body, it should still allow all request content types defined in the OpenAPI document.

Let's assume the following OpenAPI document:

openapi: 3.1.0

info:
  title: OpenAPI document with operation that should allow both JSON and XML content types.
  version: 1.0.0

paths:
  /pets:
    post:
      x-symfony-controller: 'Nijens\OpenapiBundle\Tests\Functional\App\Controller\CreatePetController'
      summary: Add a new pet to the store.
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
          application/xml:
            schema:
              $ref: '#/components/schemas/Pet'
        required: true
      responses:
        '201':
          description: Successfully added a new pet to the store.

# ...

The above document should allow both application/json and application/xml content types when loaded by the bundle.

Remove support for x-symfony-controller specification extension

In version 1.4.0 the x-openapi-bundle specification extension was introduced to add additional configuration values to operations in OpenAPI documents.

With version 2.0.0, support for the x-symfony-controller specification extension will be removed from the bundle.

Deprecate x-symfony-controller specification extension

In version 1.4.0 the x-openapi-bundle specification extension was introduced to add additional configuration values to operations in OpenAPI documents.

With version 1.5.0, the x-symfony-controller specification extension will be marked as deprecated.

Use operationId as Symfony route name

Currently, the routes created by the bundle aren't easily referenced when generating an URL through a Symfony UrlGeneratorInterface implementation as they're automatically generated from the path.

By using the operationId from the OpenAPI specification we'd be able to reference routes the way we're used to with Symfony.

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.