Skip to content

OData formatted OpenAPI Spec validations not working #126

@Rishabh-Daga

Description

@Rishabh-Daga

When attempting to validate an OData formatted OpenAPI specification using the libopenapi-validator library, the validation fails. The issue seems to be related to the handling of OData path parameters and their specific formatting.

Steps to Reproduce:

  1. Run the below program.
package main

import (
	"fmt"
	"github.com/pb33f/libopenapi"
	validator "github.com/pb33f/libopenapi-validator"
	"net/http"
)

const openAPISpec = `{
  "openapi": "3.0.0",
  "info": {
    "title": "Sample OData Service",
    "version": "1.0.0"
  },
  "paths": {
    "/orders(OrderID='{OrderID}',OrderDate=datetime'{OrderDate}')": {
      "parameters": [
        {
          "name": "OrderID",
          "in": "path",
          "required": true,
          "schema": {
            "type": "integer",
            "format": "uint8",
          }
        },
        {
          "name": "OrderDate",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "responses": {
          "200": {
            "description": "Retrieved entity"
          }
        }
      }
    }
  }
}`

func main() {

	document, err := libopenapi.NewDocument([]byte(openAPISpec))
	if err != nil {
		panic(err)
	}

	_, errs := document.BuildV3Model()
	if len(errs) != 0 {
		panic(errs[0])
	}

	libopenapiValidator, errs := validator.NewValidator(document)
	if len(errs) != 0 {
		panic(errs[0])
	}

	invalidPathReq, _ := http.NewRequest(http.MethodGet, "/invalid", nil)
	isSuccess, valErrs := libopenapiValidator.ValidateHttpRequest(invalidPathReq) // this should fail due to invalid path
	if !isSuccess {
		panic("Error in validating path: " + valErrs[0].Error())
	}
	fmt.Println("Path validation successful")

	invalidParameterReq, _ := http.NewRequest(http.MethodGet, "/orders(OrderID='abc',OrderDate=datetime'1492041600000')", nil)
	isSuccess, valErrs = libopenapiValidator.GetParameterValidator().ValidatePathParams(invalidParameterReq) // this should fail due to invalid parameter
	if !isSuccess {
		panic("Error in validating path: " + valErrs[0].Error())
	}
	fmt.Println("Parameter validation successful")
}

Expected Behavior:

Both validations should fail.

Actual Behavior:

The validations pass, indicating an issue with the handling of OData path and path parameters.

Possible enhancements

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions