Skip to content
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

Address #78 #82

Closed
wants to merge 1 commit into from
Closed

Address #78 #82

wants to merge 1 commit into from

Conversation

budanm
Copy link

@budanm budanm commented May 7, 2024

@daveshanley - For a path parameter which is marked as required , empty value should not be allowed. Ideally , in this case , we do not need to proceed towards enum check as the absence of a value should violate the mandatory path parameter requirement criteria irrespective of the schema of path parameter

For a path parameter which is marked as required , empty value should not be allowed. Ideally , in this case , we do not need to proceed towards enum check as the absence of a value should violate the mandatory path parameter requirement criteria irrespective of the schema of path parameter
@budanm
Copy link
Author

budanm commented May 7, 2024

@daveshanley - i ran all the tests locally and it had passed , not sure what did I miss

@daveshanley
Copy link
Member

Run go test ./...
  go test ./...
  shell: /usr/bin/bash -e {0}
?   	github.com/pb33f/libopenapi-validator/errors	[no test files]
?   	github.com/pb33f/libopenapi-validator/helpers	[no test files]
--- FAIL: ExampleNewValidator_validateHttpRequest (0.0[2](https://github.com/pb33f/libopenapi-validator/actions/runs/8981876020/job/24693187697?pr=82#step:5:2)s)
got:
  Type: parameter, Failure: Path parameter 'petId' is not a valid number
  Type: security, Failure: API Key api_key not found in header
want:
  Type: security, Failure: API Key api_key not found in header
  Type: parameter, Failure: Path parameter 'petId' is not a valid number
FAIL
FAIL	github.com/pb[3](https://github.com/pb33f/libopenapi-validator/actions/runs/8981876020/job/24693187697?pr=82#step:5:3)3f/libopenapi-validator	0.68[4](https://github.com/pb33f/libopenapi-validator/actions/runs/8981876020/job/24693187697?pr=82#step:5:5)s
ok  	github.com/pb33f/libopenapi-validator/parameters	0.183s
ok  	github.com/pb33f/libopenapi-validator/paths	0.128s
ok  	github.com/pb33f/libopenapi-validator/requests	0.0[6](https://github.com/pb33f/libopenapi-validator/actions/runs/8981876020/job/24693187697?pr=82#step:5:7)3s
ok  	github.com/pb33f/libopenapi-validator/responses	0.042s
?   	github.com/pb33f/libopenapi-validator/schema_validation/openapi_schemas	[no test files]
ok  	github.com/pb33f/libopenapi-validator/schema_validation	0.06[8](https://github.com/pb33f/libopenapi-validator/actions/runs/8981876020/job/24693187697?pr=82#step:5:9)s
FAIL
Error: Process completed with exit code 1.

@budanm
Copy link
Author

budanm commented May 8, 2024

Hi @daveshanley

I ran go test ./... multiple times and I still see no failures. From the errors , it looks like the order of errors has been reversed in the output of the test.

got:
  Type: parameter, Failure: Path parameter 'petId' is not a valid number
  Type: security, Failure: API Key api_key not found in header
want:
  Type: security, Failure: API Key api_key not found in header
  Type: parameter, Failure: Path parameter 'petId' is not a valid number

However , on running this particular test multiple number of times locally , i get the expected outcome. Would it be possible for you to retrigger this workflow ? Do we need to have such assertions on the order of errors considering the fact that the list will contain all the errors ?

@triptesh1212
Copy link

Hi @daveshanley, I have been analyzing the failed test case. It can not be deterministic because the error messages are added to the error array from an async method.
https://github.com/pb33f/libopenapi-validator/blob/6a51b954a34c886fc665b938eb0c8f3367438c9f/validator.go#L193C4-L193C5

Here, I have modified that test to run multiple times and check how many times its failing.

func TestNewValidator_PetStore_PetGet200_PathNotFound(t *testing.T) {

func TestNewValidator_PetStore_PetGet200_PathNotFound(t *testing.T) {

	// create a new document from the petstore spec
	doc, _ := libopenapi.NewDocument(petstoreBytes)

	// create a doc
	v, _ := NewValidator(doc)

	// create a pet
	body := map[string]interface{}{
		"id":   123,
		"name": "cotton",
		"category": map[string]interface{}{
			"id":   123,
			"name": "dogs",
		},
		"photoUrls": []string{"https://example.com"},
	}

	// marshal the body into bytes.
	bodyBytes, _ := json.Marshal(body) // operation returns pet

	// create a new put request
	request, _ := http.NewRequest(http.MethodGet,
		"https://hyperspace-superherbs.com/pet/IamNotANumber", nil)
	request.Header.Set("Content-Type", "application/json")

	// simulate a request/response, in this case the contract returns a 200 with the pet we just created.
	res := httptest.NewRecorder()
	handler := func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set(helpers.ContentTypeHeader, helpers.JSONContentType)
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write(bodyBytes)
	}

	// fire the request
	handler(res, request)

	noOfSuccessfulTest := 0

	for i := 0; i < 500; i++ {
		// validate the response
		valid, errors := v.ValidateHttpRequestResponse(request, res.Result())

		assert.False(t, valid)
		assert.Len(t, errors, 2)

		if strings.EqualFold(errors[0].Message, "API Key api_key not found in header") {
			noOfSuccessfulTest = noOfSuccessfulTest + 1
		}

		assert.Equal(t, "API Key api_key not found in header", errors[0].Message)
		assert.Equal(t, "Path parameter 'petId' is not a valid number", errors[1].Message)
	}

	fmt.Println("noOfFailedTest = ", 500-noOfSuccessfulTest)
}

noOfFailedTest has different values while running the test multiple times. Sometimes 5 number of times its failing out of 500, sometimes 4 out of 500.

So, I guess the test case is not valid.

Thanks,
Triptesh

@daveshanley
Copy link
Member

already merged.

@daveshanley daveshanley closed this May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants