|
7 | 7 | "bytes" |
8 | 8 | "encoding/json" |
9 | 9 | "fmt" |
| 10 | + "github.com/pb33f/libopenapi-validator/config" |
10 | 11 | "net/http" |
11 | 12 | "testing" |
12 | 13 |
|
@@ -1444,3 +1445,52 @@ func TestValidateBody_SchemaNoType_Issue75(t *testing.T) { |
1444 | 1445 | assert.Len(t, valErrs, 1) |
1445 | 1446 | assert.Equal(t, "PUT request body is empty for '/path1'", valErrs[0].Message) |
1446 | 1447 | } |
| 1448 | + |
| 1449 | +// https://github.com/pb33f/libopenapi-validator/issues/144 |
| 1450 | +func TestValidateBody_InvalidSchema_EnsureOptionsPassthrough(t *testing.T) { |
| 1451 | + spec := `openapi: 3.1.0 |
| 1452 | +paths: |
| 1453 | + /burgers/createBurger: |
| 1454 | + post: |
| 1455 | + requestBody: |
| 1456 | + content: |
| 1457 | + application/json: |
| 1458 | + schema: |
| 1459 | + $ref: '#/components/schema_validation/V1_UserRequest' |
| 1460 | +components: |
| 1461 | + schema_validation: |
| 1462 | + V1_UserRequest: |
| 1463 | + type: object |
| 1464 | + properties: |
| 1465 | + email: |
| 1466 | + type: string |
| 1467 | + format: email |
| 1468 | + minLength: 1 |
| 1469 | + maxLength: 320` |
| 1470 | + |
| 1471 | + doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1472 | + |
| 1473 | + m, _ := doc.BuildV3Model() |
| 1474 | + v := NewRequestBodyValidator(&m.Model, config.WithFormatAssertions()) |
| 1475 | + |
| 1476 | + items := make(map[string]interface{}) |
| 1477 | + items["email"] = "test" |
| 1478 | + |
| 1479 | + bodyBytes, _ := json.Marshal(items) |
| 1480 | + |
| 1481 | + request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger", |
| 1482 | + bytes.NewBuffer(bodyBytes)) |
| 1483 | + request.Header.Set("Content-Type", "application/json") |
| 1484 | + |
| 1485 | + valid, errors := v.ValidateRequestBody(request) |
| 1486 | + |
| 1487 | + assert.False(t, valid) |
| 1488 | + assert.Len(t, errors, 1) |
| 1489 | + assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 1490 | + assert.Equal(t, "POST request body for '/burgers/createBurger' failed to validate schema", errors[0].Message) |
| 1491 | + |
| 1492 | + assert.False(t, valid) |
| 1493 | + assert.Len(t, errors, 1) |
| 1494 | + assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 1495 | + assert.Equal(t, "'test' is not valid email: missing @", errors[0].SchemaValidationErrors[0].Reason) |
| 1496 | +} |
0 commit comments