Skip to content

Commit

Permalink
add support for servers on operation level for 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorelli92 committed Nov 5, 2024
1 parent ef38f54 commit 4cfd0b1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ The following annotations are only available if you set the -v3.1 flag in the CL
| x-name | The extension key, must be start by x- and take only json value. |
| x-codeSample | Optional Markdown usage. take `file` as parameter. This will then search for a file named like the summary in the given folder. |
| deprecated | Mark endpoint as deprecated. |
| servers.url | (Only for -v3.1 on the CLI) The URL of a server that will override the base one for this operation |
| servers.description | (Only for -v3.1 on the CLI) The description of a server that will override the base one for this operation |
## Mime Types
Expand Down Expand Up @@ -993,7 +994,7 @@ If the struct is defined in a dependency package, use `--parseDependency`.
If the struct is defined in your main project, use `--parseInternal`.
if you want to include both internal and from dependencies use both flags
if you want to include both internal and from dependencies use both flags
```
swag init --parseDependency --parseInternal
```
Expand Down
17 changes: 17 additions & 0 deletions operationv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (o *OperationV3) ParseComment(comment string, astFile *ast.File) error {
o.Deprecated = true
case xCodeSamplesAttr, xCodeSamplesAttrOriginal:
return o.ParseCodeSample(attribute, commentLine, lineRemainder)
case "@servers.url":
return o.ParseServerURLComment(lineRemainder)
case "@servers.description":
return o.ParseServerDescriptionComment(lineRemainder)
default:
return o.ParseMetadata(attribute, lowerAttribute, lineRemainder)
}
Expand Down Expand Up @@ -741,6 +745,19 @@ func (o *OperationV3) ParseRouterComment(commentLine string) error {
return nil
}

func (o *OperationV3) ParseServerURLComment(commentLine string) error {
server := spec.NewServer()
server.Spec.URL = commentLine
o.Servers = append(o.Servers, server)
return nil
}

func (o *OperationV3) ParseServerDescriptionComment(commentLine string) error {
lastAddedServer := o.Servers[len(o.Servers)-1]
lastAddedServer.Spec.Description = commentLine
return nil
}

// createParameter returns swagger spec.Parameter for given paramType, description, paramName, schemaType, required.
func createParameterV3(in, description, paramName, objectType, schemaType string, required bool, enums []interface{}, collectionFormat string) spec.Parameter {
// //five possible parameter types. query, path, body, header, form
Expand Down
28 changes: 28 additions & 0 deletions operationv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2054,3 +2054,31 @@ func TestProcessProduceComment(t *testing.T) {
content = operation.Responses.Spec.Response["500"].Spec.Spec.Content
assert.Nil(t, content)
}

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

operation := NewOperationV3(nil)

comment := `/@servers.url https://api.example.com/v1`
err := operation.ParseComment(comment, nil)
require.NoError(t, err)

comment = `/@servers.description override path 1`
err = operation.ParseComment(comment, nil)
require.NoError(t, err)

comment = `/@servers.url https://api.example.com/v2`
err = operation.ParseComment(comment, nil)
require.NoError(t, err)

comment = `/@servers.description override path 2`
err = operation.ParseComment(comment, nil)
require.NoError(t, err)

assert.Len(t, operation.Servers, 2)
assert.Equal(t, "https://api.example.com/v1", operation.Servers[0].Spec.URL)
assert.Equal(t, "override path 1", operation.Servers[0].Spec.Description)
assert.Equal(t, "https://api.example.com/v2", operation.Servers[1].Spec.URL)
assert.Equal(t, "override path 2", operation.Servers[1].Spec.Description)
}

0 comments on commit 4cfd0b1

Please sign in to comment.