GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Clarify absence of `OapiResponseValidator` and make middleware options more specific · Issue #1038 · oapi-codegen/oapi-codegen

Clarify absence of OapiResponseValidator and make middleware options more specific #1038

New issue
Copy link
New issue
Copy link
Open
Open
Clarify absence of OapiResponseValidator and make middleware options more specific#1038
Copy link

Description

@Antonboom
Antonboom
opened on Apr 28, 2023
Issue body actions

Hello!

OapiRequestValidatorWithOptions has ambiguous API. Not all fields from openapi3filter.Options are used:

oapimdlwr.OapiRequestValidatorWithOptions(v1Swagger, &oapimdlwr.Options{
    Options: openapi3filter.Options{
        ExcludeRequestBody:  false,
        ExcludeResponseBody: true, // Doesn't matter!
        AuthenticationFunc:  openapi3filter.NoopAuthenticationFunc,
    },
})

My bad, but I was sure that the oapi-codegen could validate the response.
And was surprised to find that there is no use of the openapi3filter.ValidateResponse.

Reacted by Tim Neumann

Activity

  1. sirockin commented on May 9, 2023

    @sirockin
    sirockin
    on May 9, 2023
    More actions

    Hi Anton. I've been looking at this too as I'm also interested in response validation. It looks as if the options struct is imported from a different package github.com/getkin/kin-openapi which the middleware in this package uses to carry out validations. As you mention the response-related flags are not used into the RequestValidator.

    It looks like it would be useful to either:

    • Extend the OapiRequestValidator{WithOptions} methods to include response validation when specified (although this would make the name misleading and create a breaking change)
    • Add method(s) OapiResponseValidator{WithOptions} which would call the kin-openapi response validation code
    Reacted by Anton Telyshev, John Behm, Andrew, Masato Sogame and Logan Griswold
  2. zucchinho commented on Feb 23, 2024

    @zucchinho
    zucchinho
    on Feb 23, 2024
    More actions

    i'm going to look at creating this middleware, will update here when i have a PR ready

    Reacted by Anton Telyshev
  3. zucchinho commented on Mar 15, 2024

    @zucchinho
    zucchinho
    on Mar 15, 2024
    More actions

    i've got the bones of this for gin (which is the http router we use) but it could easily be repurposed to different implementations oapi-codegen/gin-middleware#13

    Reacted by Tim Neumann
  4. jamietanna commented on Mar 16, 2024

    @jamietanna
    jamietanna
    on Mar 16, 2024
    Member
    More actions

    Thanks for the work on this! What use cases do you have for this? Would be interested to know just so I can gauge how it's going to be used.

    Ie should it be configurable that it actually stops the malformed response going to the caller or should it just ie log?

    For testing purposes I wrote https://pkg.go.dev/openapi.tanna.dev/go/validator which may be helpful but if you want the full app integrated then the proposed solution looks good.

  5. zucchinho commented on Mar 18, 2024

    @zucchinho
    zucchinho
    on Mar 18, 2024
    More actions

    Essentially this is for use in conjunction with the OapiRequestValidator, which actually validates incoming requests against the schema. If you have defined responses in an OAS schema, and the responses your application responds with do not conform to that contract, then ultimately your API is breaking its contract, and I would view this as a server error (hence the 500 status code). It does somewhat defeat the point of defining responses in an API spec if the endpoints can return data which doesn't obey the schema.

    Having it be configurable in that sense is not something I'd considered, but would be desirable. For some use cases, having such a strict all or nothing approach to response validation is overkill, as the consumer might not be interested in the areas where the schema is violated. So I can get behind providing the option of hooking in with a log-only implementation.

    Testing schema compliance before releases is a sensible thing to do in its own right, and ultimately just as (if not more) important as having this type of middleware running in the deployed API. That tool looks like a really good approach, I'll definitely check it out.

    Reacted by jamfor999
  6. zucchinho commented on Mar 22, 2024

    @zucchinho
    zucchinho
    on Mar 22, 2024
    More actions

    I realise actually that it readily configurable using the ErrorHandler field in the options, which can be used to override the status code when the response violates the schema. This is also where any custom logging can be set up.

    To maintain the consistency with the request middleware, I'd say this is the best approach for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.