Skip to content

1957: Support for extensions within swagger:route #2751

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

Merged

Conversation

Huckletoon
Copy link
Contributor

Summary

Adding basic functionality to support the definition of vendor extensions on a per-path basis as requested in issue #1957. This would be defined by a developer within a swagger:route block of their codebase, and the corresponding swagger definition would be created when running swagger generate spec. These changes currently only support go1.11+.

These changes are making the following assumptions:

  • the value of a vendor extension can be a single string, a list/array of strings, or objects
  • a list cannot contain a mix of strings and objects
  • annotations defining extensions should be indented properly
  • each extension key must start with x-, e.g. x-some-extension
  • objects defined in an extension may contain multiple other nested objects

The new extension parser reads through the Extension annotation block within a swagger:route annotation, checking for what kind of data each line may be and builds out as many map[string]interface{}s as necessary until reaching the end of the block.

  • Simple key:value extensions will be map[string]string
  • Lists/Arrays will be map[string]*[]string
  • Objects will be map[string]interface{} that can contain any of the above types, including more objects

Example

Input:

// swagger:route GET /pets pets users listPets
//
// Lists pets filtered by some parameters.
//
//     Extensions:
//       x-example-flag: true
//       x-some-expected-list:
//         - dog
//         - cat
//         - bird
//       x-env-data:
//         userId:
//           prod: some-user-id
//           dev: some-test-id

Output:

paths:
  "/pets":
    get:
      operationId: listPets
      summary: Lists pets filtered by some parameters.
      tags:
      - pets
      - users
      extensions:
        x-example-flag: true
        x-some-list:
        - dog
        - cat
        - bird
        x-env-data:
          userId:
            prod: some-user-id
            dev: some-test-id

Fixes #1957

Adding basic functionality to support the definition of vendor extensions on a per-route basis.

Signed-off-by: Gary Ramos <huckletoon@gmail.com>
Add unit tests for route extension parsing
@codecov
Copy link

codecov bot commented Mar 30, 2022

Codecov Report

Merging #2751 (d4dc0b3) into master (a543a92) will increase coverage by 0.07%.
The diff coverage is 90.26%.

@@            Coverage Diff             @@
##           master    #2751      +/-   ##
==========================================
+ Coverage   82.77%   82.85%   +0.07%     
==========================================
  Files          55       55              
  Lines        9597     9710     +113     
==========================================
+ Hits         7944     8045     +101     
- Misses       1131     1138       +7     
- Partials      522      527       +5     
Impacted Files Coverage Δ
codescan/parser.go 80.50% <89.81%> (+1.24%) ⬆️
codescan/routes.go 95.83% <100.00%> (+0.48%) ⬆️
generator/structs.go 83.24% <0.00%> (-0.56%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a543a92...d4dc0b3. Read the comment docs.

@casualjim casualjim merged commit 42c184c into go-swagger:master Mar 30, 2022
@casualjim
Copy link
Member

Thanks. nice piece of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for extensions within swagger:route
3 participants