Skip to content

Commit

Permalink
test: check unsupported bodyRequest format
Browse files Browse the repository at this point in the history
  • Loading branch information
aizlee committed Sep 20, 2024
1 parent c69c90b commit 601a8fa
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Validators/SwaggerSpecValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ protected function validateResponse(array $response, string $statusCode, string
array_merge(self::SCHEMA_TYPES, ['file']),
"{$responseId}.schema"
);
}

if (!empty($response['items'])) {
$this->validateItems($response['items'], "{$responseId}.items");
if (!empty($response['schema']['items'])) {
$this->validateItems($response['schema']['items'], "{$responseId}.schema.items");
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ public static function getConstructorInvalidTmpData(): array
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. Field 'securityDefinitions.0.in' has an invalid value: invalid. Allowed values: query, header.",
],
[
'tmpDoc' => 'documentation/invalid_format__request_body__invalid_content',
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. Operation 'paths./users/{id}.post' has body parameters. Only one or the other is allowed.",
],
[
'tmpDoc' => 'documentation/invalid_format__response__invalid_items',
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. 'paths./users/{id}.post.responses.200.schema.items' should have required fields: type.",
],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Invalid API"
},
"paths": {
"/users/{id}": {
"post": {
"consumes": [],
"produces": [
"application/json"
],
"parameters": [
{
"in": "path",
"name": "id",
"description": "",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"image/png": {
"items": []
}
},
"required": true,
"description": ""
},
"responses": {
"default": {
"description": "hello world"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore 2.0"
},
"host": "petstore.swagger.io",
"basePath": "/v2",
"tags": [
{
"name": "pet",
"description": "Everything about your Pets",
"externalDocs": {
"description": "Find out more",
"url": "http://swagger.io"
}
},
{
"name": "store",
"description": "Access to Petstore orders"
},
{
"name": "user",
"description": "Operations about user",
"externalDocs": {
"description": "Find out more about our store",
"url": "http://swagger.io"
}
}
],
"schemes": [
"https",
"http"
],
"paths": {
"/users/{id}": {
"post": {
"tags": [
"pet"
],
"summary": "Finds Pets by status",
"description": "Multiple status values can be provided with comma separated strings",
"operationId": "findPetsByStatus",
"produces": [
"application/xml",
"application/json"
],
"parameters": [
{
"in": "path",
"name": "id",
"description": "",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Pet"
}
}
},
"400": {
"description": "Invalid status value"
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
]
}
}
},
"securityDefinitions": {
"petstore_auth": {
"type": "oauth2",
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
"flow": "implicit",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
},
"definitions": {
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"
},
"quantity": {
"type": "integer",
"format": "int32"
},
"shipDate": {
"type": "string",
"format": "date-time"
},
"status": {
"type": "string",
"description": "Order Status",
"enum": [
"placed",
"approved",
"delivered"
]
},
"complete": {
"type": "boolean",
"default": false
}
},
"xml": {
"name": "Order"
}
},
"Category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Category"
}
},
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"username": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"email": {
"type": "string"
},
"password": {
"type": "string"
},
"phone": {
"type": "string"
},
"userStatus": {
"type": "integer",
"format": "int32",
"description": "User Status"
}
},
"xml": {
"name": "User"
}
},
"Tag": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"xml": {
"name": "Tag"
}
},
"Pet": {
"type": "object",
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"xml": {
"name": "tag",
"wrapped": true
},
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
},
"xml": {
"name": "Pet"
}
}
}
}

0 comments on commit 601a8fa

Please sign in to comment.