Skip to content

Commit 4412f17

Browse files
Merge pull request #712 from jaredhendrickson13/next_patch
v2.5.1 Fixes
2 parents 378b604 + f88c932 commit 4412f17

File tree

5 files changed

+68
-11
lines changed

5 files changed

+68
-11
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/ContentHandler.inc

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,46 @@ class ContentHandler {
269269
* @return array This ContentHandler as an OpenAPI 'content' schema for this MIME type in a given Response object.
270270
*/
271271
public function to_openapi_schema(Response $response, Endpoint $endpoint): array {
272-
# Format the data schema based on the endpoint for successful responses
273-
if ($response instanceof Success and $endpoint->many) {
272+
# Default to an empty schema in the event we cannot determine the schema
273+
$data_schema = [
274+
'oneOf' => [['type' => 'array', 'items' => ['type' => 'object']], ['type' => 'object']],
275+
];
276+
277+
# Ensure the 'id' field is included for success responses on many models without a parent
278+
if ($response instanceof Success and $endpoint->model->many and !$endpoint->model->parent_model_class) {
274279
$data_schema = [
275-
'type' => 'array',
276-
'items' => ['$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}"],
280+
'allOf' => [
281+
['type' => 'object', 'properties' => ['id' => ['type' => $endpoint->model->id_type]]],
282+
['$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}"],
283+
],
277284
];
278-
} elseif ($response instanceof Success and !$endpoint->many) {
285+
}
286+
# Ensure the 'parent_id' and 'id' fields are included for success responses on many models with a parent
287+
elseif ($response instanceof Success and $endpoint->model->many and $endpoint->model->parent_model_class) {
288+
$parent_type = $endpoint->model->get_parent_model()->id_type;
289+
$data_schema = [
290+
'allOf' => [
291+
[
292+
'type' => 'object',
293+
'properties' => [
294+
'parent_id' => ['type' => $parent_type],
295+
'id' => ['type' => $endpoint->model->id_type],
296+
],
297+
],
298+
['$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}"],
299+
],
300+
];
301+
}
302+
# Do not include any extra fields for non many models
303+
elseif ($response instanceof Success and !$endpoint->model->many) {
279304
$data_schema = ['$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}"];
280-
} else {
305+
}
306+
307+
# If this is a many endpoint, nest the data schema in an array
308+
if ($response instanceof Success and $endpoint->many) {
281309
$data_schema = [
282-
'oneOf' => [['type' => 'array', 'items' => ['type' => 'object']], ['type' => 'object']],
310+
'type' => 'array',
311+
'items' => $data_schema,
283312
];
284313
}
285314

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/NetworkInterface.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class NetworkInterface extends Model {
165165
$this->gateway = new ForeignModelField(
166166
model_name: ['RoutingGateway', 'RoutingGatewayGroup'],
167167
model_field: 'name',
168-
model_query: ['ipprotocol' => 'inet', 'interface' => &$this->id],
168+
model_query: ['ipprotocol' => 'inet'],
169169
allow_null: true,
170170
conditions: ['typev4' => 'static'],
171171
help_text: 'Sets the upstream gateway this interface will use. This is only applicable for WAN-type interfaces.',
@@ -313,7 +313,7 @@ class NetworkInterface extends Model {
313313
$this->gatewayv6 = new ForeignModelField(
314314
model_name: ['RoutingGateway', 'RoutingGatewayGroup'],
315315
model_field: 'name',
316-
model_query: ['ipprotocol' => 'inet6', 'interface' => &$this->id],
316+
model_query: ['ipprotocol' => 'inet6'],
317317
allow_null: true,
318318
conditions: ['typev6' => 'staticv6'],
319319
help_text: 'Sets the upstream IPv6 gateway this interface will use. This is only applicable for WAN-type interfaces.',

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/RoutingGatewayGroupPriority.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RoutingGatewayGroupPriority extends Model {
2222
$this->parent_model_class = 'RoutingGatewayGroup';
2323
$this->config_path = 'item';
2424
$this->subsystem = 'staticroutes';
25+
$this->update_strategy = 'replace';
2526
$this->many = true;
2627
$this->many_minimum = 1;
2728

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APICoreContentHandlerTestCase.inc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,17 @@ class APICoreContentHandlerTestCase extends TestCase {
136136
'data' => [
137137
'type' => 'array',
138138
'items' => [
139-
'$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}",
139+
'allOf' => [
140+
[
141+
'type' => 'object',
142+
'properties' => [
143+
'id' => ['type' => $endpoint->model->id_type],
144+
],
145+
],
146+
[
147+
'$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}",
148+
],
149+
],
140150
],
141151
],
142152
],
@@ -165,7 +175,17 @@ class APICoreContentHandlerTestCase extends TestCase {
165175
[
166176
'type' => 'object',
167177
'properties' => [
168-
'data' => ['$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}"],
178+
'data' => [
179+
'allOf' => [
180+
[
181+
'type' => 'object',
182+
'properties' => [
183+
'id' => ['type' => $endpoint->model->id_type],
184+
],
185+
],
186+
['$ref' => "#/components/schemas/{$endpoint->model->get_class_shortname()}"],
187+
],
188+
],
169189
],
170190
],
171191
],

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APIModelsRoutingGatewayGroupPriorityTestCase.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class APIModelsRoutingGatewayGroupPriorityTestCase extends TestCase {
5858
$gateway_prio->tier->value = 3;
5959
$gateway_prio->update();
6060

61+
# Ensure the priority was actually updated
62+
$this->assert_equals(
63+
RoutingGatewayGroupPriority::query(parent_id: $gateway_group->id, id: $gateway_prio->id)->first()->tier
64+
->value,
65+
3,
66+
);
67+
6168
# Delete the gateway group priority object and ensure it was deleted
6269
$gateway_prio->delete();
6370
$this->assert_equals(RoutingGatewayGroupPriority::read_all(parent_id: $gateway_group->id)->count(), 1);

0 commit comments

Comments
 (0)