Skip to content

Commit 104a118

Browse files
committed
Address issue #789
1 parent 81f251c commit 104a118

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

model/utils.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import (
1212

1313
const (
1414
OAS2 = "oas2"
15-
OAS3 = "oas3"
15+
OAS3 = "oas3" // family format - matches all 3.x versions
16+
OAS30 = "oas3_0" // exact 3.0 only - does not match 3.1 or 3.2
1617
OAS31 = "oas3_1"
1718
OAS32 = "oas3_2"
1819
)
1920

2021
var OAS3_1Format = []string{OAS31}
2122
var OAS3_2Format = []string{OAS32}
22-
var AllExceptOAS3_1 = []string{OAS2, OAS3}
23+
var AllExceptOAS3_1 = []string{OAS2, OAS30} // uses OAS30 to avoid matching 3.1
2324
var OAS3Format = []string{OAS3}
2425
var OAS3AllFormat = []string{OAS3, OAS31, OAS32}
2526
var OAS2Format = []string{OAS2}
@@ -29,6 +30,7 @@ var AllFormats = []string{OAS3, OAS31, OAS32, OAS2}
2930
// The oas3 format is treated as a "family" that covers oas3, oas3_1, and oas3_2.
3031
// This allows rules with `formats: [oas3]` to match OpenAPI 3.0, 3.1, and 3.2 specs,
3132
// which matches Spectral's behavior.
33+
// The oas3_0 format is an exact match for OpenAPI 3.0 only (does not match 3.1 or 3.2).
3234
func FormatMatches(ruleFormat, specFormat string) bool {
3335
if ruleFormat == specFormat {
3436
return true
@@ -37,6 +39,10 @@ func FormatMatches(ruleFormat, specFormat string) bool {
3739
if ruleFormat == OAS3 && (specFormat == OAS31 || specFormat == OAS32) {
3840
return true
3941
}
42+
// oas3_0 (exact 3.0) matches when spec is detected as oas3 (which means 3.0)
43+
if ruleFormat == OAS30 && specFormat == OAS3 {
44+
return true
45+
}
4046
return false
4147
}
4248

model/utils_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,12 @@ func TestFormatMatches(t *testing.T) {
583583
{"oas2 does not match oas3_1", OAS2, OAS31, false},
584584
{"oas2 does not match oas3_2", OAS2, OAS32, false},
585585

586+
// oas3_0 (exact 3.0) matching - only matches 3.0, not 3.1 or 3.2
587+
{"oas3_0 matches oas3 (spec detected as 3.0)", OAS30, OAS3, true},
588+
{"oas3_0 does not match oas3_1", OAS30, OAS31, false},
589+
{"oas3_0 does not match oas3_2", OAS30, OAS32, false},
590+
{"oas3_0 does not match oas2", OAS30, OAS2, false},
591+
586592
// Edge cases with empty strings
587593
{"empty rule format does not match oas3", "", OAS3, false},
588594
{"oas3 does not match empty spec format", OAS3, "", false},

0 commit comments

Comments
 (0)