Skip to content

Commit 6e44a1c

Browse files
committed
fix: 0 values in conditions_v2 (jianyuan#575)
There is [arguably a bug in the Sentry API](https://github.com/getsentry/sentry/blob/7b8d66494904659365bbc0e5b37db029d72ef36d/src/sentry/api/serializers/rest_framework/rule.py#L40-L42) where integer values of 0 will be stripped from submitted payload objects because of Python's implicit bool conversions. These values can successfully be submitted as strings. When these objects are retrieved from the API, the values are integers. To model this, we have two different schemas in the OpenAPI spec yaml for rule conditions based based on whether we're sending them to the API or retriving them from the API: * `ProjectRuleCondition`, which is a set of condition schemas where `value`s are `integers` * `ProjectRuleConditionToApi` which is a set of condition schemas where `value`s are strings The generated types from these are used in the alert rule resource code in the `Fill` and `ToApi` methods as necessary. There may be a way to do this without so much duplication, I am definitely open to suggestions!
1 parent 70f9275 commit 6e44a1c

File tree

6 files changed

+581
-53
lines changed

6 files changed

+581
-53
lines changed

internal/apiclient/api.yaml

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ paths:
670670
conditions:
671671
type: array
672672
items:
673-
$ref: "#/components/schemas/ProjectRuleCondition"
673+
$ref: "#/components/schemas/ProjectRuleConditionToApi"
674674
filters:
675675
type: array
676676
items:
@@ -767,7 +767,7 @@ paths:
767767
conditions:
768768
type: array
769769
items:
770-
$ref: "#/components/schemas/ProjectRuleCondition"
770+
$ref: "#/components/schemas/ProjectRuleConditionToApi"
771771
filters:
772772
type: array
773773
items:
@@ -1327,6 +1327,14 @@ components:
13271327
type: array
13281328
items:
13291329
$ref: "#/components/schemas/ProjectRuleAction"
1330+
1331+
# There is arguably a bug in the Sentry API where integer values of 0 will
1332+
# be stripped from submitted payload objects because of Python's implicit
1333+
# bool conversions. These values can successfully be submitted as strings.
1334+
# When these objects are retrieved from the API, the values are integers.
1335+
# To model this, we have two different schemas for rule conditions based
1336+
# based on whether we're sending them to the API or retriving them from
1337+
# the API: ProjectRuleCondition and ProjectRuleConditionToApi.
13301338
ProjectRuleCondition:
13311339
oneOf:
13321340
- $ref: "#/components/schemas/ProjectRuleConditionFirstSeenEvent"
@@ -1348,6 +1356,28 @@ components:
13481356
"sentry.rules.conditions.event_frequency.EventFrequencyCondition": "#/components/schemas/ProjectRuleConditionEventFrequency"
13491357
"sentry.rules.conditions.event_frequency.EventUniqueUserFrequencyCondition": "#/components/schemas/ProjectRuleConditionEventUniqueUserFrequency"
13501358
"sentry.rules.conditions.event_frequency.EventFrequencyPercentCondition": "#/components/schemas/ProjectRuleConditionEventFrequencyPercent"
1359+
1360+
ProjectRuleConditionToApi:
1361+
oneOf:
1362+
- $ref: "#/components/schemas/ProjectRuleConditionFirstSeenEvent"
1363+
- $ref: "#/components/schemas/ProjectRuleConditionRegressionEvent"
1364+
- $ref: "#/components/schemas/ProjectRuleConditionReappearedEvent"
1365+
- $ref: "#/components/schemas/ProjectRuleConditionNewHighPriorityIssue"
1366+
- $ref: "#/components/schemas/ProjectRuleConditionExistingHighPriorityIssue"
1367+
- $ref: "#/components/schemas/ProjectRuleConditionEventFrequencyToApi"
1368+
- $ref: "#/components/schemas/ProjectRuleConditionEventUniqueUserFrequencyToApi"
1369+
- $ref: "#/components/schemas/ProjectRuleConditionEventFrequencyPercentToApi"
1370+
discriminator:
1371+
propertyName: id
1372+
mapping:
1373+
"sentry.rules.conditions.first_seen_event.FirstSeenEventCondition": "#/components/schemas/ProjectRuleConditionFirstSeenEvent"
1374+
"sentry.rules.conditions.regression_event.RegressionEventCondition": "#/components/schemas/ProjectRuleConditionRegressionEvent"
1375+
"sentry.rules.conditions.reappeared_event.ReappearedEventCondition": "#/components/schemas/ProjectRuleConditionReappearedEvent"
1376+
"sentry.rules.conditions.high_priority_issue.NewHighPriorityIssueCondition": "#/components/schemas/ProjectRuleConditionNewHighPriorityIssue"
1377+
"sentry.rules.conditions.high_priority_issue.ExistingHighPriorityIssueCondition": "#/components/schemas/ProjectRuleConditionExistingHighPriorityIssue"
1378+
"sentry.rules.conditions.event_frequency.EventFrequencyCondition": "#/components/schemas/ProjectRuleConditionEventFrequencyToApi"
1379+
"sentry.rules.conditions.event_frequency.EventUniqueUserFrequencyCondition": "#/components/schemas/ProjectRuleConditionEventUniqueUserFrequencyToApi"
1380+
"sentry.rules.conditions.event_frequency.EventFrequencyPercentCondition": "#/components/schemas/ProjectRuleConditionEventFrequencyPercentToApi"
13511381
ProjectRuleConditionFirstSeenEvent:
13521382
type: object
13531383
required:
@@ -1426,6 +1456,28 @@ components:
14261456
format: int64
14271457
interval:
14281458
type: string
1459+
ProjectRuleConditionEventFrequencyToApi:
1460+
type: object
1461+
required:
1462+
- id
1463+
- comparisonType
1464+
- value
1465+
- interval
1466+
properties:
1467+
id:
1468+
type: string
1469+
enum:
1470+
- "sentry.rules.conditions.event_frequency.EventFrequencyCondition"
1471+
name:
1472+
type: string
1473+
comparisonType:
1474+
type: string
1475+
comparisonInterval:
1476+
type: string
1477+
value:
1478+
type: string
1479+
interval:
1480+
type: string
14291481
ProjectRuleConditionEventUniqueUserFrequency:
14301482
type: object
14311483
required:
@@ -1449,6 +1501,28 @@ components:
14491501
format: int64
14501502
interval:
14511503
type: string
1504+
ProjectRuleConditionEventUniqueUserFrequencyToApi:
1505+
type: object
1506+
required:
1507+
- id
1508+
- comparisonType
1509+
- value
1510+
- interval
1511+
properties:
1512+
id:
1513+
type: string
1514+
enum:
1515+
- "sentry.rules.conditions.event_frequency.EventUniqueUserFrequencyCondition"
1516+
name:
1517+
type: string
1518+
comparisonType:
1519+
type: string
1520+
comparisonInterval:
1521+
type: string
1522+
value:
1523+
type: string
1524+
interval:
1525+
type: string
14521526
ProjectRuleConditionEventFrequencyPercent:
14531527
type: object
14541528
required:
@@ -1472,6 +1546,28 @@ components:
14721546
format: double
14731547
interval:
14741548
type: string
1549+
ProjectRuleConditionEventFrequencyPercentToApi:
1550+
type: object
1551+
required:
1552+
- id
1553+
- comparisonType
1554+
- value
1555+
- interval
1556+
properties:
1557+
id:
1558+
type: string
1559+
enum:
1560+
- "sentry.rules.conditions.event_frequency.EventFrequencyPercentCondition"
1561+
name:
1562+
type: string
1563+
comparisonType:
1564+
type: string
1565+
comparisonInterval:
1566+
type: string
1567+
value:
1568+
type: string
1569+
interval:
1570+
type: string
14751571
ProjectRuleFilter:
14761572
oneOf:
14771573
- $ref: "#/components/schemas/ProjectRuleFilterAgeComparison"

0 commit comments

Comments
 (0)