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

fixed operation when there is a newline character between annotations #1846

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package swag

import (
"bufio"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -385,6 +386,47 @@ func (parser *Parser) skipPackageByPrefix(pkgpath string) bool {
return true
}

func parseAnnotations(packageDir, searchDir string) error {
// Combine the packageDir and searchDir to locate the files
files, err := filepath.Glob(filepath.Join(packageDir, searchDir, "*.go"))
if err != nil {
return fmt.Errorf("error finding files in directory %s: %w", searchDir, err)
}

for _, file := range files {
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("error opening file %s: %w", file, err)
}
defer f.Close()

scanner := bufio.NewScanner(f)
previousLineWasAnnotation := false

for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)

if strings.HasPrefix(trimmed, "//") {
// This is an annotation
if previousLineWasAnnotation && len(trimmed) == 2 {
// The previous line was an annotation, and this line is a newline
return fmt.Errorf("newline detected between annotations in file %s, which is not allowed", file)
}
previousLineWasAnnotation = true
} else {
previousLineWasAnnotation = false
}
}

if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading annotations in file %s: %w", file, err)
}
}

return nil
}

// ParseAPIMultiSearchDir is like ParseAPI but for multiple search dirs.
func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile string, parseDepth int) error {
for _, searchDir := range searchDirs {
Expand All @@ -399,6 +441,10 @@ func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile st
if err != nil {
return err
}

if err := parseAnnotations(packageDir, searchDir); err != nil {
return err
}
}

absMainAPIFilePath, err := filepath.Abs(filepath.Join(searchDirs[0], mainAPIFile))
Expand Down
51 changes: 51 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package swag

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"go/ast"
goparser "go/parser"
"go/token"
Expand Down Expand Up @@ -4373,6 +4375,55 @@ func Test(){
assert.NotNil(t, val2.Get)
}

// Test function for parser
func TestParser_ParseAnnotations(t *testing.T) {
t.Run("NoNewlineBetweenAnnotations", func(t *testing.T) {
content := `// @annotation1
// @annotation2`
err := testParseAnnotations(content)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})

t.Run("NewlineBetweenAnnotations", func(t *testing.T) {
content := `// @annotation1

// @annotation2`
err := testParseAnnotations(content)
if err == nil {
t.Fatalf("expected an error due to newline between annotations, but got nil")
}
})
}

// Helper function to simulate annotation parsing and detect newline issues
func testParseAnnotations(content string) error {
scanner := bufio.NewScanner(strings.NewReader(content))
previousLineWasAnnotation := false

for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)

if strings.HasPrefix(trimmed, "//") {
// Detected an annotation
if previousLineWasAnnotation && len(trimmed) == 2 {
// Newline detected between annotations
return fmt.Errorf("newline detected between annotations, which is not allowed")
}
previousLineWasAnnotation = true
} else if trimmed == "" && previousLineWasAnnotation {
// Newline following an annotation
return fmt.Errorf("newline detected between annotations")
} else {
previousLineWasAnnotation = false
}
}

return scanner.Err()
}

func TestParser_EmbeddedStructAsOtherAliasGoListNested(t *testing.T) {
t.Parallel()

Expand Down