Skip to content

feat: add /spec endpoint to OAS APIs to expose OAS definitions #7030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gateway/api_loader.go
Original file line number Diff line number Diff line change
@@ -118,6 +118,9 @@ func fixFuncPath(pathPrefix string, funcs []apidef.MiddlewareDefinition) {
}

func (gw *Gateway) generateSubRoutes(spec *APISpec, router *mux.Router, logger *logrus.Entry) {
// Add OAS spec endpoint for OAS APIs
gw.addOASSpecEndpoint(spec, router, logger)

if spec.GraphQL.GraphQLPlayground.Enabled {
gw.loadGraphQLPlayground(spec, router)
}
43 changes: 43 additions & 0 deletions gateway/oas_spec_endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gateway

import (
"encoding/json"
"net/http"

"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)

const (
oasSpecEndpoint = "/spec"
)

// addOASSpecEndpoint adds a /spec endpoint to OAS APIs that returns the OAS definition
// This endpoint bypasses all middleware, including authentication, rate limiting, etc.
func (gw *Gateway) addOASSpecEndpoint(spec *APISpec, router *mux.Router, logger *logrus.Entry) {
// Only add the endpoint to OAS APIs
if !spec.IsOAS {
return
}

logger.Debug("Adding OAS spec endpoint for API: ", spec.APIID)

// Create a direct handler for the /spec endpoint that completely bypasses middleware
// This is registered directly with the router, so it's processed before any middleware chain
router.HandleFunc(oasSpecEndpoint, func(w http.ResponseWriter, r *http.Request) {
// Set content type header
w.Header().Set("Content-Type", "application/json")

// Marshal the OAS definition to JSON
oasJSON, err := json.Marshal(spec.OAS)
if err != nil {
logger.WithError(err).Error("Error marshaling OAS spec to JSON")
http.Error(w, "Error generating OAS spec", http.StatusInternalServerError)
return
}

// Write the response
w.WriteHeader(http.StatusOK)
w.Write(oasJSON)
}).Methods(http.MethodGet)
}