Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.16.2 #614

Merged
merged 11 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .github/sponsors/apideck-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/sponsors/apideck-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed .github/sponsors/zuplo-dark.png
Binary file not shown.
Binary file removed .github/sponsors/zuplo-light.png
Binary file not shown.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ like our _very kind_ sponsors:

[scalar](https://scalar.com)

<a href="https://apideck.com">
<picture>
<source media="(prefers-color-scheme: dark)" srcset=".github/sponsors/apideck-dark.png">
<img alt="apideck'" src=".github/sponsors/apideck-light.png">
</picture>
</a>

[apideck](https://apideck.com)


---

## Come chat with us
Expand Down
2 changes: 2 additions & 0 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func GetBundleCommand() *cobra.Command {
noStyleFlag, _ := cmd.Flags().GetBool("no-style")
baseFlag, _ := cmd.Flags().GetString("base")
remoteFlag, _ := cmd.Flags().GetBool("remote")
extensionRefsFlag, _ := cmd.Flags().GetBool("ext-refs")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -115,6 +116,7 @@ func GetBundleCommand() *cobra.Command {
ExtractRefsSequentially: true,
Logger: logger,
AllowRemoteReferences: remoteFlag,
ExcludeExtensionRefs: extensionRefsFlag,
}

bundled, err := bundler.BundleBytes(specBytes, docConfig)
Expand Down
5 changes: 5 additions & 0 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func GetDashboardCommand() *cobra.Command {
timeoutFlag, _ := cmd.Flags().GetInt("timeout")
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
silent, _ := cmd.Flags().GetBool("silent")
extensionRefsFlag, _ := cmd.Flags().GetBool("ext-refs")

var err error
vacuumReport, specBytes, _ := vacuum_report.BuildVacuumReportFromFile(args[0])
Expand Down Expand Up @@ -102,6 +103,10 @@ func GetDashboardCommand() *cobra.Command {
config.AllowRemoteLookup = true
}

if extensionRefsFlag {
config.ExcludeExtensionRefs = true
}

specIndex = index.NewSpecIndexWithConfig(&rootNode, config)

specInfo = vacuumReport.SpecInfo
Expand Down
2 changes: 2 additions & 0 deletions cmd/language_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ IDE and start linting your OpenAPI documents in real-time.`,
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
ignoreArrayCircleRef, _ := cmd.Flags().GetBool("ignore-array-circle-ref")
ignorePolymorphCircleRef, _ := cmd.Flags().GetBool("ignore-array-circle-ref")
extensionRefsFlag, _ := cmd.Flags().GetBool("ext-refs")

defaultRuleSets := rulesets.BuildDefaultRuleSetsWithLogger(logger)
selectedRS := defaultRuleSets.GenerateOpenAPIRecommendedRuleSet()
Expand Down Expand Up @@ -81,6 +82,7 @@ IDE and start linting your OpenAPI documents in real-time.`,
IgnoreArrayCircleRef: ignoreArrayCircleRef,
IgnorePolymorphCircleRef: ignorePolymorphCircleRef,
Logger: logger,
ExtensionRefs: extensionRefsFlag,
}

return languageserver.NewServer(Version, &lfr).Run()
Expand Down
27 changes: 15 additions & 12 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func GetLintCommand() *cobra.Command {
timeoutFlag, _ := cmd.Flags().GetInt("timeout")
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
noClipFlag, _ := cmd.Flags().GetBool("no-clip")
extensionRefsFlag, _ := cmd.Flags().GetBool("ext-refs")
ignoreArrayCircleRef, _ := cmd.Flags().GetBool("ignore-array-circle-ref")
ignorePolymorphCircleRef, _ := cmd.Flags().GetBool("ignore-polymorph-circle-ref")
ignoreFile, _ := cmd.Flags().GetString("ignore-file")
Expand Down Expand Up @@ -238,6 +239,7 @@ func GetLintCommand() *cobra.Command {
IgnoreArrayCircleRef: ignoreArrayCircleRef,
IgnorePolymorphCircleRef: ignorePolymorphCircleRef,
IgnoredResults: ignoredItems,
ExtensionRefs: extensionRefsFlag,
}
fs, fp, err := lintFile(lfr)

Expand Down Expand Up @@ -338,18 +340,19 @@ func lintFile(req utils.LintFileRequest) (int64, int, error) {
}

result := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
RuleSet: req.SelectedRS,
Spec: specBytes,
SpecFileName: req.FileName,
CustomFunctions: req.Functions,
Base: req.BaseFlag,
AllowLookup: req.Remote,
SkipDocumentCheck: req.SkipCheckFlag,
Logger: req.Logger,
BuildDeepGraph: deepGraph,
Timeout: time.Duration(req.TimeoutFlag) * time.Second,
IgnoreCircularArrayRef: req.IgnoreArrayCircleRef,
IgnoreCircularPolymorphicRef: req.IgnorePolymorphCircleRef,
RuleSet: req.SelectedRS,
Spec: specBytes,
SpecFileName: req.FileName,
CustomFunctions: req.Functions,
Base: req.BaseFlag,
AllowLookup: req.Remote,
SkipDocumentCheck: req.SkipCheckFlag,
Logger: req.Logger,
BuildDeepGraph: deepGraph,
Timeout: time.Duration(req.TimeoutFlag) * time.Second,
IgnoreCircularArrayRef: req.IgnoreArrayCircleRef,
IgnoreCircularPolymorphicRef: req.IgnorePolymorphCircleRef,
ExtractReferencesFromExtensions: req.ExtensionRefs,
})

result.Results = filterIgnoredResults(result.Results, req.IgnoredResults)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func GetRootCommand() *cobra.Command {
rootCmd.PersistentFlags().BoolP("debug", "w", false, "Turn on debug logging")
rootCmd.PersistentFlags().IntP("timeout", "g", 5, "Rule timeout in seconds, default is 5 seconds")
rootCmd.PersistentFlags().BoolP("hard-mode", "z", false, "Enable all the built-in rules, even the OWASP ones. This is the level to beat!")
rootCmd.PersistentFlags().BoolP("ext-refs", "", false, "Turn on $ref lookups and resolving for extensions (x-) objects")

if regErr := rootCmd.RegisterFlagCompletionFunc("functions", cobra.FixedCompletions(
[]string{"so"}, cobra.ShellCompDirectiveFilterFileExt,
Expand Down
16 changes: 9 additions & 7 deletions cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetSpectralReportCommand() *cobra.Command {
skipCheckFlag, _ := cmd.Flags().GetBool("skip-check")
timeoutFlag, _ := cmd.Flags().GetInt("timeout")
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
extensionRefsFlag, _ := cmd.Flags().GetBool("ext-refs")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -151,13 +152,14 @@ func GetSpectralReportCommand() *cobra.Command {
}

ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
Base: baseFlag,
SkipDocumentCheck: skipCheckFlag,
Timeout: time.Duration(timeoutFlag) * time.Second,
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
Base: baseFlag,
SkipDocumentCheck: skipCheckFlag,
Timeout: time.Duration(timeoutFlag) * time.Second,
ExtractReferencesFromExtensions: extensionRefsFlag,
})

resultSet := model.NewRuleResultSet(ruleset.Results)
Expand Down
18 changes: 10 additions & 8 deletions cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func GetVacuumReportCommand() *cobra.Command {
timeoutFlag, _ := cmd.Flags().GetInt("timeout")
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
ignoreFile, _ := cmd.Flags().GetString("ignore-file")
extensionRefsFlag, _ := cmd.Flags().GetBool("ext-refs")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -172,14 +173,15 @@ func GetVacuumReportCommand() *cobra.Command {
}

ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
Base: baseFlag,
SkipDocumentCheck: skipCheckFlag,
BuildDeepGraph: deepGraph,
Timeout: time.Duration(timeoutFlag) * time.Second,
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
Base: baseFlag,
SkipDocumentCheck: skipCheckFlag,
BuildDeepGraph: deepGraph,
Timeout: time.Duration(timeoutFlag) * time.Second,
ExtractReferencesFromExtensions: extensionRefsFlag,
})

resultSet := model.NewRuleResultSet(ruleset.Results)
Expand Down
123 changes: 114 additions & 9 deletions functions/openapi/examples_missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
continue
}

if (p.SchemaProxy != nil && p.SchemaProxy.Schema != nil && p.SchemaProxy.Schema.Value != nil) &&
(p.SchemaProxy.Schema.Value.Const != nil || p.SchemaProxy.Schema.Value.Default != nil) {
continue
}

if p.SchemaProxy != nil {
if len(p.SchemaProxy.Schema.Value.Type) <= 0 {
continue
Expand Down Expand Up @@ -159,6 +164,11 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
continue
}

if (h.Schema != nil && h.Schema.Schema != nil && h.Schema.Schema.Value != nil) &&
(h.Schema.Schema.Value.Const != nil || h.Schema.Schema.Value.Default != nil) {
continue
}

if h.Schema != nil {
if len(h.Schema.Schema.Value.Type) <= 0 {
continue
Expand Down Expand Up @@ -212,7 +222,43 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
continue
}

if mt.Value.Examples.Len() <= 0 && isExampleNodeNull([]*yaml.Node{mt.Value.Example}) {
if (mt.SchemaProxy != nil && mt.SchemaProxy.Schema != nil && mt.SchemaProxy.Schema.Value != nil) &&
(mt.SchemaProxy.Schema.Value.Const != nil || mt.SchemaProxy.Schema.Value.Default != nil) {
continue
}

propErr := false
hasProps := false
if mt.SchemaProxy != nil && mt.SchemaProxy.Schema.Properties != nil && mt.SchemaProxy.Schema.Properties.Len() > 0 {
hasProps = true
var prop *base.Schema
var propName string
for k, v := range mt.SchemaProxy.Schema.Properties.FromOldest() {
if !checkProps(v.Schema) {
propErr = true
prop = v.Schema
propName = k
break
}
}
if propErr {
path := prop.GenerateJSONPath()
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message,
fmt.Sprintf("media type schema property `%s` is missing `examples` or `example`", propName)),
path,
prop.KeyNode, mt.ValueNode, mt))
}
}

buf.WriteString(fmt.Sprintf("%s:%d:%d", mt.Value.GoLow().GetIndex().GetSpecAbsolutePath(),
mt.Value.GoLow().KeyNode.Line, mt.Value.GoLow().KeyNode.Column))
if _, ok := seen[buf.String()]; !ok {
seen[buf.String()] = true
}
buf.Reset()

if hasProps && propErr && mt.Value.Examples.Len() <= 0 && isExampleNodeNull([]*yaml.Node{mt.Value.Example}) {

n := mt.Value.GoLow().RootNode
if mt.Value.GoLow().KeyNode != nil {
Expand All @@ -225,13 +271,23 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, "media type is missing `examples` or `example`"),
path,
n, mt.ValueNode, mt))
} else {
buf.WriteString(fmt.Sprintf("%s:%d:%d", mt.Value.GoLow().GetIndex().GetSpecAbsolutePath(),
mt.Value.GoLow().KeyNode.Line, mt.Value.GoLow().KeyNode.Column))
if _, ok := seen[buf.String()]; !ok {
seen[buf.String()] = true
continue
}

if !hasProps && mt.Value.Examples.Len() <= 0 && isExampleNodeNull([]*yaml.Node{mt.Value.Example}) {

n := mt.Value.GoLow().RootNode
if mt.Value.GoLow().KeyNode != nil {
if mt.Value.GoLow().KeyNode.Line == n.Line-1 {
n = mt.Value.GoLow().KeyNode
}
}
buf.Reset()
path := mt.GenerateJSONPath()
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, "media type is missing `examples` or `example`"),
path,
n, mt.ValueNode, mt))
continue
}
}
}
Expand All @@ -243,12 +299,17 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
continue
}

if (s.Value != nil) &&
(s.Value.Const != nil || s.Value.Default != nil) {
continue
}

if len(s.Value.Type) <= 0 {
continue
}

if s.Value.Items != nil && s.Value.Items.IsA() && s.Value.Items.A != nil {
if len(s.Value.Items.A.Schema().Enum) > 0 {
if s.Value.Items.A.Schema() != nil && len(s.Value.Items.A.Schema().Enum) > 0 {
continue
}
}
Expand All @@ -267,12 +328,42 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
}
}

if isExampleNodeNull(s.Value.Examples) && isExampleNodeNull([]*yaml.Node{s.Value.Example}) {
propErr := false
hasProps := false
if s.Properties != nil && s.Properties.Len() > 0 {
hasProps = true
var prop *base.Schema
var propName string
for k, v := range s.Properties.FromOldest() {
if !checkProps(v.Schema) {
propErr = true
prop = v.Schema
propName = k
break
}
}
if propErr {
path := prop.GenerateJSONPath()
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message,
fmt.Sprintf("schema property `%s` is missing `examples` or `example`", propName)),
path,
prop.KeyNode, s.ValueNode, s))
}
}

if hasProps && propErr && isExampleNodeNull(s.Value.Examples) && isExampleNodeNull([]*yaml.Node{s.Value.Example}) {
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, "schema is missing `examples` or `example`"),
s.GenerateJSONPath(),
s.Value.ParentProxy.GetSchemaKeyNode(), s.Value.ParentProxy.GetValueNode(), s))
}

if !hasProps && isExampleNodeNull(s.Value.Examples) && isExampleNodeNull([]*yaml.Node{s.Value.Example}) {
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, "schema is missing `examples` or `example`"),
s.GenerateJSONPath(),
s.Value.ParentProxy.GetSchemaKeyNode(), s.Value.ParentProxy.GetValueNode(), s))
}
}
}
Expand All @@ -281,6 +372,20 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
return results
}

func checkProps(s *base.Schema) bool {
if len(s.Value.Examples) > 0 {
return true
}
if s.Value.Example != nil {
return true
}
if s.Value.Properties != nil && s.Value.Properties.Len() > 0 {
for _, p := range s.Properties.FromOldest() {
return checkProps(p.Schema)
}
}
return false
}
func checkParent(s any, depth int) bool {
if depth > 10 {
return false
Expand Down
6 changes: 3 additions & 3 deletions functions/openapi/examples_missing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ paths:
def := ExamplesMissing{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 2)
assert.Len(t, res, 1)
assert.Equal(t, "media type is missing `examples` or `example`", res[0].Message)
assert.Contains(t, res[0].Path, "$.paths['/pizza'].get.requestBody.content['application/json']")
}
Expand Down Expand Up @@ -178,8 +178,8 @@ components:
def := ExamplesMissing{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "schema is missing `examples` or `example`", res[0].Message)
assert.Len(t, res, 2)
assert.Equal(t, "schema property `id` is missing `examples` or `example`", res[0].Message)
assert.Contains(t, res[0].Path, "$.components.schemas['Pizza']")
}

Expand Down
Loading