├── .github ├── CONTRIBUTING.md ├── dependabot.yaml └── workflows │ ├── auto-merge.yml │ └── go-test.yml ├── .gitignore ├── .golangci.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── internal ├── normalize_url.go └── normalize_url_test.go ├── reference.go └── reference_test.go /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contribution Guidelines 2 | 3 | ### Pull requests are always welcome 4 | 5 | We are always thrilled to receive pull requests, and do our best to 6 | process them as fast as possible. Not sure if that typo is worth a pull 7 | request? Do it! We will appreciate it. 8 | 9 | If your pull request is not accepted on the first try, don't be 10 | discouraged! If there's a problem with the implementation, hopefully you 11 | received feedback on what to improve. 12 | 13 | We're trying very hard to keep go-swagger lean and focused. We don't want it 14 | to do everything for everybody. This means that we might decide against 15 | incorporating a new feature. However, there might be a way to implement 16 | that feature *on top of* go-swagger. 17 | 18 | 19 | ### Conventions 20 | 21 | Fork the repo and make changes on your fork in a feature branch: 22 | 23 | - If it's a bugfix branch, name it XXX-something where XXX is the number of the 24 | issue 25 | - If it's a feature branch, create an enhancement issue to announce your 26 | intentions, and name it XXX-something where XXX is the number of the issue. 27 | 28 | Submit unit tests for your changes. Go has a great test framework built in; use 29 | it! Take a look at existing tests for inspiration. Run the full test suite on 30 | your branch before submitting a pull request. 31 | 32 | Update the documentation when creating or modifying features. Test 33 | your documentation changes for clarity, concision, and correctness, as 34 | well as a clean documentation build. See ``docs/README.md`` for more 35 | information on building the docs and how docs get released. 36 | 37 | Write clean code. Universally formatted code promotes ease of writing, reading, 38 | and maintenance. Always run `gofmt -s -w file.go` on each changed file before 39 | committing your changes. Most editors have plugins that do this automatically. 40 | 41 | Pull requests descriptions should be as clear as possible and include a 42 | reference to all the issues that they address. 43 | 44 | Pull requests must not contain commits from other users or branches. 45 | 46 | Commit messages must start with a capitalized and short summary (max. 50 47 | chars) written in the imperative, followed by an optional, more detailed 48 | explanatory text which is separated from the summary by an empty line. 49 | 50 | Code review comments may be added to your pull request. Discuss, then make the 51 | suggested modifications and push additional commits to your feature branch. Be 52 | sure to post a comment after pushing. The new commits will show up in the pull 53 | request automatically, but the reviewers will not be notified unless you 54 | comment. 55 | 56 | Before the pull request is merged, make sure that you squash your commits into 57 | logical units of work using `git rebase -i` and `git push -f`. After every 58 | commit the test suite should be passing. Include documentation changes in the 59 | same commit so that a revert would remove all traces of the feature or fix. 60 | 61 | Commits that fix or close an issue should include a reference like `Closes #XXX` 62 | or `Fixes #XXX`, which will automatically close the issue when merged. 63 | 64 | ### Sign your work 65 | 66 | The sign-off is a simple line at the end of the explanation for the 67 | patch, which certifies that you wrote it or otherwise have the right to 68 | pass it on as an open-source patch. The rules are pretty simple: if you 69 | can certify the below (from 70 | [developercertificate.org](http://developercertificate.org/)): 71 | 72 | ``` 73 | Developer Certificate of Origin 74 | Version 1.1 75 | 76 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 77 | 660 York Street, Suite 102, 78 | San Francisco, CA 94110 USA 79 | 80 | Everyone is permitted to copy and distribute verbatim copies of this 81 | license document, but changing it is not allowed. 82 | 83 | 84 | Developer's Certificate of Origin 1.1 85 | 86 | By making a contribution to this project, I certify that: 87 | 88 | (a) The contribution was created in whole or in part by me and I 89 | have the right to submit it under the open source license 90 | indicated in the file; or 91 | 92 | (b) The contribution is based upon previous work that, to the best 93 | of my knowledge, is covered under an appropriate open source 94 | license and I have the right under that license to submit that 95 | work with modifications, whether created in whole or in part 96 | by me, under the same open source license (unless I am 97 | permitted to submit under a different license), as indicated 98 | in the file; or 99 | 100 | (c) The contribution was provided directly to me by some other 101 | person who certified (a), (b) or (c) and I have not modified 102 | it. 103 | 104 | (d) I understand and agree that this project and the contribution 105 | are public and that a record of the contribution (including all 106 | personal information I submit with it, including my sign-off) is 107 | maintained indefinitely and may be redistributed consistent with 108 | this project or the open source license(s) involved. 109 | ``` 110 | 111 | then you just add a line to every git commit message: 112 | 113 | Signed-off-by: Joe Smith 114 | 115 | using your real name (sorry, no pseudonyms or anonymous contributions.) 116 | 117 | You can add the sign off when creating the git commit via `git commit -s`. 118 | -------------------------------------------------------------------------------- /.github/dependabot.yaml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | day: "friday" 8 | open-pull-requests-limit: 2 # <- default is 5 9 | groups: # <- group all github actions updates in a single PR 10 | # 1. development-dependencies are auto-merged 11 | development-dependencies: 12 | patterns: 13 | - '*' 14 | 15 | - package-ecosystem: "gomod" 16 | # We define 4 groups of dependencies to regroup update pull requests: 17 | # - development (e.g. test dependencies) 18 | # - go-openapi updates 19 | # - golang.org (e.g. golang.org/x/... packages) 20 | # - other dependencies (direct or indirect) 21 | # 22 | # * All groups are checked once a week and each produce at most 1 PR. 23 | # * All dependabot PRs are auto-approved 24 | # 25 | # Auto-merging policy, when requirements are met: 26 | # 1. development-dependencies are auto-merged 27 | # 2. golang.org-dependencies are auto-merged 28 | # 3. go-openapi patch updates are auto-merged. Minor/major version updates require a manual merge. 29 | # 4. other dependencies require a manual merge 30 | directory: "/" 31 | schedule: 32 | interval: "weekly" 33 | day: "friday" 34 | open-pull-requests-limit: 4 35 | groups: 36 | development-dependencies: 37 | patterns: 38 | - "github.com/stretchr/testify" 39 | 40 | golang.org-dependencies: 41 | patterns: 42 | - "golang.org/*" 43 | 44 | go-openapi-dependencies: 45 | patterns: 46 | - "github.com/go-openapi/*" 47 | 48 | other-dependencies: 49 | exclude-patterns: 50 | - "github.com/go-openapi/*" 51 | - "github.com/stretchr/testify" 52 | - "golang.org/*" 53 | -------------------------------------------------------------------------------- /.github/workflows/auto-merge.yml: -------------------------------------------------------------------------------- 1 | name: Dependabot auto-merge 2 | on: pull_request 3 | 4 | permissions: 5 | contents: write 6 | pull-requests: write 7 | 8 | jobs: 9 | dependabot: 10 | runs-on: ubuntu-latest 11 | if: github.event.pull_request.user.login == 'dependabot[bot]' 12 | steps: 13 | - name: Dependabot metadata 14 | id: metadata 15 | uses: dependabot/fetch-metadata@v2 16 | 17 | - name: Auto-approve all dependabot PRs 18 | run: gh pr review --approve "$PR_URL" 19 | env: 20 | PR_URL: ${{github.event.pull_request.html_url}} 21 | GH_TOKEN: ${{secrets.GITHUB_TOKEN}} 22 | 23 | - name: Auto-merge dependabot PRs for development dependencies 24 | if: contains(steps.metadata.outputs.dependency-group, 'development-dependencies') 25 | run: gh pr merge --auto --rebase "$PR_URL" 26 | env: 27 | PR_URL: ${{github.event.pull_request.html_url}} 28 | GH_TOKEN: ${{secrets.GITHUB_TOKEN}} 29 | 30 | - name: Auto-merge dependabot PRs for go-openapi patches 31 | if: contains(steps.metadata.outputs.dependency-group, 'go-openapi-dependencies') && (steps.metadata.outputs.update-type == 'version-update:semver-minor' || steps.metadata.outputs.update-type == 'version-update:semver-patch') 32 | run: gh pr merge --auto --rebase "$PR_URL" 33 | env: 34 | PR_URL: ${{github.event.pull_request.html_url}} 35 | GH_TOKEN: ${{secrets.GITHUB_TOKEN}} 36 | 37 | - name: Auto-merge dependabot PRs for golang.org updates 38 | if: contains(steps.metadata.outputs.dependency-group, 'golang.org-dependencies') 39 | run: gh pr merge --auto --rebase "$PR_URL" 40 | env: 41 | PR_URL: ${{github.event.pull_request.html_url}} 42 | GH_TOKEN: ${{secrets.GITHUB_TOKEN}} 43 | 44 | -------------------------------------------------------------------------------- /.github/workflows/go-test.yml: -------------------------------------------------------------------------------- 1 | name: go test 2 | 3 | on: 4 | push: 5 | tags: 6 | - v* 7 | branches: 8 | - master 9 | 10 | pull_request: 11 | 12 | jobs: 13 | lint: 14 | name: Lint 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-go@v5 19 | with: 20 | go-version: stable 21 | check-latest: true 22 | cache: true 23 | - name: golangci-lint 24 | uses: golangci/golangci-lint-action@v8 25 | with: 26 | version: latest 27 | only-new-issues: true 28 | skip-cache: true 29 | 30 | test: 31 | name: Unit tests 32 | runs-on: ${{ matrix.os }} 33 | 34 | strategy: 35 | matrix: 36 | os: [ ubuntu-latest, macos-latest, windows-latest ] 37 | go_version: ['oldstable', 'stable' ] 38 | 39 | steps: 40 | - uses: actions/setup-go@v5 41 | with: 42 | go-version: '${{ matrix.go_version }}' 43 | check-latest: true 44 | cache: true 45 | 46 | - uses: actions/checkout@v4 47 | - name: Run unit tests 48 | shell: bash 49 | run: go test -v -race -coverprofile="coverage-${{ matrix.os }}.${{ matrix.go_version }}.out" -covermode=atomic -coverpkg=$(go list)/... ./... 50 | 51 | - name: Upload coverage to codecov 52 | uses: codecov/codecov-action@v5 53 | with: 54 | files: './coverage-${{ matrix.os }}.${{ matrix.go_version }}.out' 55 | flags: '${{ matrix.go_version }}-${{ matrix.os }}' 56 | fail_ci_if_error: false 57 | verbose: true 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | secrets.yml 2 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | linters: 3 | default: all 4 | disable: 5 | - cyclop 6 | - depguard 7 | - errchkjson 8 | - errorlint 9 | - exhaustruct 10 | - forcetypeassert 11 | - funlen 12 | - gochecknoglobals 13 | - gochecknoinits 14 | - gocognit 15 | - godot 16 | - godox 17 | - gosmopolitan 18 | - inamedparam 19 | - ireturn 20 | - lll 21 | - musttag 22 | - nestif 23 | - nlreturn 24 | - nonamedreturns 25 | - paralleltest 26 | - testpackage 27 | - thelper 28 | - tparallel 29 | - unparam 30 | - varnamelen 31 | - whitespace 32 | - wrapcheck 33 | - wsl 34 | settings: 35 | dupl: 36 | threshold: 200 37 | goconst: 38 | min-len: 2 39 | min-occurrences: 3 40 | gocyclo: 41 | min-complexity: 45 42 | exclusions: 43 | generated: lax 44 | presets: 45 | - comments 46 | - common-false-positives 47 | - legacy 48 | - std-error-handling 49 | paths: 50 | - third_party$ 51 | - builtin$ 52 | - examples$ 53 | formatters: 54 | enable: 55 | - gofmt 56 | - goimports 57 | exclusions: 58 | generated: lax 59 | paths: 60 | - third_party$ 61 | - builtin$ 62 | - examples$ 63 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at ivan+abuse@flanders.co.nz. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gojsonreference [![Build Status](https://github.com/go-openapi/jsonreference/actions/workflows/go-test.yml/badge.svg)](https://github.com/go-openapi/jsonreference/actions?query=workflow%3A"go+test") [![codecov](https://codecov.io/gh/go-openapi/jsonreference/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/jsonreference) 2 | 3 | [![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io) 4 | [![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/jsonreference/master/LICENSE) 5 | [![Go Reference](https://pkg.go.dev/badge/github.com/go-openapi/jsonreference.svg)](https://pkg.go.dev/github.com/go-openapi/jsonreference) 6 | [![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/jsonreference)](https://goreportcard.com/report/github.com/go-openapi/jsonreference) 7 | 8 | An implementation of JSON Reference - Go language 9 | 10 | ## Status 11 | Feature complete. Stable API 12 | 13 | ## Dependencies 14 | * https://github.com/go-openapi/jsonpointer 15 | 16 | ## References 17 | 18 | * http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07 19 | * http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03 20 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/go-openapi/jsonreference 2 | 3 | require ( 4 | github.com/go-openapi/jsonpointer v0.21.1 5 | github.com/stretchr/testify v1.10.0 6 | ) 7 | 8 | require ( 9 | github.com/davecgh/go-spew v1.1.1 // indirect 10 | github.com/go-openapi/swag v0.23.1 // indirect 11 | github.com/josharian/intern v1.0.0 // indirect 12 | github.com/mailru/easyjson v0.9.0 // indirect 13 | github.com/pmezard/go-difflib v1.0.0 // indirect 14 | github.com/rogpeppe/go-internal v1.11.0 // indirect 15 | gopkg.in/yaml.v3 v3.0.1 // indirect 16 | ) 17 | 18 | go 1.20 19 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/go-openapi/jsonpointer v0.21.1 h1:whnzv/pNXtK2FbX/W9yJfRmE2gsmkfahjMKB0fZvcic= 4 | github.com/go-openapi/jsonpointer v0.21.1/go.mod h1:50I1STOfbY1ycR8jGz8DaMeLCdXiI6aDteEdRNNzpdk= 5 | github.com/go-openapi/swag v0.23.1 h1:lpsStH0n2ittzTnbaSloVZLuB5+fvSY/+hnagBjSNZU= 6 | github.com/go-openapi/swag v0.23.1/go.mod h1:STZs8TbRvEQQKUA+JZNAm3EWlgaOBGpyFDqQnDHMef0= 7 | github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= 8 | github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= 9 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 10 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 11 | github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4= 12 | github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= 13 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 14 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 15 | github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= 16 | github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= 17 | github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= 18 | github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 19 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 20 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 21 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 22 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 23 | -------------------------------------------------------------------------------- /internal/normalize_url.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "net/url" 5 | "regexp" 6 | "strings" 7 | ) 8 | 9 | const ( 10 | defaultHTTPPort = ":80" 11 | defaultHTTPSPort = ":443" 12 | ) 13 | 14 | // Regular expressions used by the normalizations 15 | var rxPort = regexp.MustCompile(`(:\d+)/?$`) 16 | var rxDupSlashes = regexp.MustCompile(`/{2,}`) 17 | 18 | // NormalizeURL will normalize the specified URL 19 | // This was added to replace a previous call to the no longer maintained purell library: 20 | // The call that was used looked like the following: 21 | // 22 | // url.Parse(purell.NormalizeURL(parsed, purell.FlagsSafe|purell.FlagRemoveDuplicateSlashes)) 23 | // 24 | // To explain all that was included in the call above, purell.FlagsSafe was really just the following: 25 | // - FlagLowercaseScheme 26 | // - FlagLowercaseHost 27 | // - FlagRemoveDefaultPort 28 | // - FlagRemoveDuplicateSlashes (and this was mixed in with the |) 29 | // 30 | // This also normalizes the URL into its urlencoded form by removing RawPath and RawFragment. 31 | func NormalizeURL(u *url.URL) { 32 | lowercaseScheme(u) 33 | lowercaseHost(u) 34 | removeDefaultPort(u) 35 | removeDuplicateSlashes(u) 36 | 37 | u.RawPath = "" 38 | u.RawFragment = "" 39 | } 40 | 41 | func lowercaseScheme(u *url.URL) { 42 | if len(u.Scheme) > 0 { 43 | u.Scheme = strings.ToLower(u.Scheme) 44 | } 45 | } 46 | 47 | func lowercaseHost(u *url.URL) { 48 | if len(u.Host) > 0 { 49 | u.Host = strings.ToLower(u.Host) 50 | } 51 | } 52 | 53 | func removeDefaultPort(u *url.URL) { 54 | if len(u.Host) > 0 { 55 | scheme := strings.ToLower(u.Scheme) 56 | u.Host = rxPort.ReplaceAllStringFunc(u.Host, func(val string) string { 57 | if (scheme == "http" && val == defaultHTTPPort) || (scheme == "https" && val == defaultHTTPSPort) { 58 | return "" 59 | } 60 | return val 61 | }) 62 | } 63 | } 64 | 65 | func removeDuplicateSlashes(u *url.URL) { 66 | if len(u.Path) > 0 { 67 | u.Path = rxDupSlashes.ReplaceAllString(u.Path, "/") 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /internal/normalize_url_test.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "net/url" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/assert" 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | func TestUrlnorm(t *testing.T) { 12 | testCases := []struct { 13 | url string 14 | expected string 15 | }{ 16 | { 17 | url: "HTTPs://xYz.cOm:443/folder//file", 18 | expected: "https://xyz.com/folder/file", 19 | }, 20 | { 21 | url: "HTTP://xYz.cOm:80/folder//file", 22 | expected: "http://xyz.com/folder/file", 23 | }, 24 | { 25 | url: "postGRES://xYz.cOm:5432/folder//file", 26 | expected: "postgres://xyz.com:5432/folder/file", 27 | }, 28 | } 29 | 30 | for _, toPin := range testCases { 31 | testCase := toPin 32 | 33 | u, err := url.Parse(testCase.url) 34 | require.NoError(t, err) 35 | 36 | NormalizeURL(u) 37 | assert.Equal(t, testCase.expected, u.String()) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /reference.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 sigu-399 ( https://github.com/sigu-399 ) 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // author sigu-399 16 | // author-github https://github.com/sigu-399 17 | // author-mail sigu.399@gmail.com 18 | // 19 | // repository-name jsonreference 20 | // repository-desc An implementation of JSON Reference - Go language 21 | // 22 | // description Main and unique file. 23 | // 24 | // created 26-02-2013 25 | 26 | package jsonreference 27 | 28 | import ( 29 | "errors" 30 | "net/url" 31 | "strings" 32 | 33 | "github.com/go-openapi/jsonpointer" 34 | "github.com/go-openapi/jsonreference/internal" 35 | ) 36 | 37 | const ( 38 | fragmentRune = `#` 39 | ) 40 | 41 | var ErrChildURL = errors.New("child url is nil") 42 | 43 | // New creates a new reference for the given string 44 | func New(jsonReferenceString string) (Ref, error) { 45 | 46 | var r Ref 47 | err := r.parse(jsonReferenceString) 48 | return r, err 49 | 50 | } 51 | 52 | // MustCreateRef parses the ref string and panics when it's invalid. 53 | // Use the New method for a version that returns an error 54 | func MustCreateRef(ref string) Ref { 55 | r, err := New(ref) 56 | if err != nil { 57 | panic(err) 58 | } 59 | return r 60 | } 61 | 62 | // Ref represents a json reference object 63 | type Ref struct { 64 | referenceURL *url.URL 65 | referencePointer jsonpointer.Pointer 66 | 67 | HasFullURL bool 68 | HasURLPathOnly bool 69 | HasFragmentOnly bool 70 | HasFileScheme bool 71 | HasFullFilePath bool 72 | } 73 | 74 | // GetURL gets the URL for this reference 75 | func (r *Ref) GetURL() *url.URL { 76 | return r.referenceURL 77 | } 78 | 79 | // GetPointer gets the json pointer for this reference 80 | func (r *Ref) GetPointer() *jsonpointer.Pointer { 81 | return &r.referencePointer 82 | } 83 | 84 | // String returns the best version of the url for this reference 85 | func (r *Ref) String() string { 86 | 87 | if r.referenceURL != nil { 88 | return r.referenceURL.String() 89 | } 90 | 91 | if r.HasFragmentOnly { 92 | return fragmentRune + r.referencePointer.String() 93 | } 94 | 95 | return r.referencePointer.String() 96 | } 97 | 98 | // IsRoot returns true if this reference is a root document 99 | func (r *Ref) IsRoot() bool { 100 | return r.referenceURL != nil && 101 | !r.IsCanonical() && 102 | !r.HasURLPathOnly && 103 | r.referenceURL.Fragment == "" 104 | } 105 | 106 | // IsCanonical returns true when this pointer starts with http(s):// or file:// 107 | func (r *Ref) IsCanonical() bool { 108 | return (r.HasFileScheme && r.HasFullFilePath) || (!r.HasFileScheme && r.HasFullURL) 109 | } 110 | 111 | // "Constructor", parses the given string JSON reference 112 | func (r *Ref) parse(jsonReferenceString string) error { 113 | 114 | parsed, err := url.Parse(jsonReferenceString) 115 | if err != nil { 116 | return err 117 | } 118 | 119 | internal.NormalizeURL(parsed) 120 | 121 | r.referenceURL = parsed 122 | refURL := r.referenceURL 123 | 124 | if refURL.Scheme != "" && refURL.Host != "" { 125 | r.HasFullURL = true 126 | } else { 127 | if refURL.Path != "" { 128 | r.HasURLPathOnly = true 129 | } else if refURL.RawQuery == "" && refURL.Fragment != "" { 130 | r.HasFragmentOnly = true 131 | } 132 | } 133 | 134 | r.HasFileScheme = refURL.Scheme == "file" 135 | r.HasFullFilePath = strings.HasPrefix(refURL.Path, "/") 136 | 137 | // invalid json-pointer error means url has no json-pointer fragment. simply ignore error 138 | r.referencePointer, _ = jsonpointer.New(refURL.Fragment) 139 | 140 | return nil 141 | } 142 | 143 | // Inherits creates a new reference from a parent and a child 144 | // If the child cannot inherit from the parent, an error is returned 145 | func (r *Ref) Inherits(child Ref) (*Ref, error) { 146 | childURL := child.GetURL() 147 | parentURL := r.GetURL() 148 | if childURL == nil { 149 | return nil, ErrChildURL 150 | } 151 | if parentURL == nil { 152 | return &child, nil 153 | } 154 | 155 | ref, err := New(parentURL.ResolveReference(childURL).String()) 156 | if err != nil { 157 | return nil, err 158 | } 159 | return &ref, nil 160 | } 161 | -------------------------------------------------------------------------------- /reference_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 sigu-399 ( https://github.com/sigu-399 ) 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // author sigu-399 16 | // author-github https://github.com/sigu-399 17 | // author-mail sigu.399@gmail.com 18 | // 19 | // repository-name jsonreference 20 | // repository-desc An implementation of JSON Reference - Go language 21 | // 22 | // description Automated tests on package. 23 | // 24 | // created 03-03-2013 25 | 26 | package jsonreference 27 | 28 | import ( 29 | "testing" 30 | 31 | "github.com/go-openapi/jsonpointer" 32 | "github.com/stretchr/testify/assert" 33 | "github.com/stretchr/testify/require" 34 | ) 35 | 36 | func TestIsRoot(t *testing.T) { 37 | t.Run("with empty fragment", func(t *testing.T) { 38 | in := "#" 39 | r1, err := New(in) 40 | require.NoError(t, err) 41 | assert.True(t, r1.IsRoot()) 42 | }) 43 | 44 | t.Run("with fragment", func(t *testing.T) { 45 | in := "#/ok" 46 | r1 := MustCreateRef(in) 47 | assert.False(t, r1.IsRoot()) 48 | }) 49 | 50 | t.Run("with invalid ref", func(t *testing.T) { 51 | assert.Panics(t, assert.PanicTestFunc(func() { 52 | MustCreateRef("%2") 53 | })) 54 | }) 55 | } 56 | 57 | func TestFullURL(t *testing.T) { 58 | t.Run("with fragment", func(t *testing.T) { 59 | const ( 60 | in = "http://host/path/a/b/c#/f/a/b" 61 | ) 62 | 63 | r1, err := New(in) 64 | require.NoError(t, err) 65 | assert.Equal(t, in, r1.String()) 66 | require.False(t, r1.HasFragmentOnly) 67 | require.True(t, r1.HasFullURL) 68 | require.False(t, r1.HasURLPathOnly) 69 | require.False(t, r1.HasFileScheme) 70 | require.Equal(t, "/f/a/b", r1.GetPointer().String()) 71 | }) 72 | 73 | t.Run("with empty fragment", func(t *testing.T) { 74 | const in = "http://host/path/a/b/c" 75 | 76 | r1, err := New(in) 77 | require.NoError(t, err) 78 | assert.Equal(t, in, r1.String()) 79 | require.False(t, r1.HasFragmentOnly) 80 | require.True(t, r1.HasFullURL) 81 | require.False(t, r1.HasURLPathOnly) 82 | require.False(t, r1.HasFileScheme) 83 | require.Empty(t, r1.GetPointer().String()) 84 | }) 85 | } 86 | 87 | func TestFragmentOnly(t *testing.T) { 88 | const in = "#/fragment/only" 89 | 90 | r1, err := New(in) 91 | require.NoError(t, err) 92 | assert.Equal(t, in, r1.String()) 93 | 94 | require.True(t, r1.HasFragmentOnly) 95 | require.False(t, r1.HasFullURL) 96 | require.False(t, r1.HasURLPathOnly) 97 | require.False(t, r1.HasFileScheme) 98 | require.Equal(t, "/fragment/only", r1.GetPointer().String()) 99 | 100 | p, err := jsonpointer.New(r1.referenceURL.Fragment) 101 | require.NoError(t, err) 102 | 103 | t.Run("Ref with fragmentOnly", func(t *testing.T) { 104 | r2 := Ref{referencePointer: p, HasFragmentOnly: true} 105 | assert.Equal(t, in, r2.String()) 106 | }) 107 | 108 | t.Run("Ref without fragmentOnly", func(t *testing.T) { 109 | r3 := Ref{referencePointer: p, HasFragmentOnly: false} 110 | assert.Equal(t, in[1:], r3.String()) 111 | }) 112 | } 113 | 114 | func TestURLPathOnly(t *testing.T) { 115 | const in = "/documents/document.json" 116 | 117 | r1, err := New(in) 118 | require.NoError(t, err) 119 | assert.Equal(t, in, r1.String()) 120 | require.False(t, r1.HasFragmentOnly) 121 | require.False(t, r1.HasFullURL) 122 | require.True(t, r1.HasURLPathOnly) 123 | require.False(t, r1.HasFileScheme) 124 | require.Empty(t, r1.GetPointer().String()) 125 | } 126 | 127 | func TestURLRelativePathOnly(t *testing.T) { 128 | const in = "document.json" 129 | 130 | r1, err := New(in) 131 | require.NoError(t, err) 132 | assert.Equal(t, in, r1.String()) 133 | require.False(t, r1.HasFragmentOnly) 134 | require.False(t, r1.HasFullURL) 135 | require.True(t, r1.HasURLPathOnly) 136 | require.False(t, r1.HasFileScheme) 137 | require.Empty(t, r1.GetPointer().String()) 138 | } 139 | 140 | func TestInheritsInValid(t *testing.T) { 141 | const ( 142 | in1 = "http://www.test.com/doc.json" 143 | in2 = "#/a/b" 144 | ) 145 | 146 | r1, err := New(in1) 147 | require.NoError(t, err) 148 | 149 | t.Run("inherits from empty Ref", func(t *testing.T) { 150 | r2 := Ref{} 151 | result, err := r1.Inherits(r2) 152 | require.Error(t, err) 153 | assert.Nil(t, result) 154 | }) 155 | 156 | t.Run("inherits from non-empty Ref", func(t *testing.T) { 157 | r1 = Ref{} 158 | r2, err := New(in2) 159 | require.NoError(t, err) 160 | 161 | result, err := r1.Inherits(r2) 162 | require.NoError(t, err) 163 | require.NotNil(t, result) 164 | assert.Equal(t, r2, *result) 165 | }) 166 | } 167 | 168 | func TestInheritsValid(t *testing.T) { 169 | const ( 170 | in1 = "http://www.test.com/doc.json" 171 | in2 = "#/a/b" 172 | out = in1 + in2 173 | ) 174 | 175 | r1, err := New(in1) 176 | require.NoError(t, err) 177 | r2, err := New(in2) 178 | require.NoError(t, err) 179 | 180 | result, err := r1.Inherits(r2) 181 | require.NoError(t, err) 182 | require.NotNil(t, result) 183 | 184 | assert.Equal(t, out, result.String()) 185 | assert.Equal(t, "/a/b", result.GetPointer().String()) 186 | } 187 | 188 | func TestInheritsDifferentHost(t *testing.T) { 189 | const ( 190 | in1 = "http://www.test.com/doc.json" 191 | in2 = "http://www.test2.com/doc.json#bla" 192 | ) 193 | 194 | r1, err := New(in1) 195 | require.NoError(t, err) 196 | r2, err := New(in2) 197 | require.NoError(t, err) 198 | 199 | result, err := r1.Inherits(r2) 200 | require.NoError(t, err) 201 | require.NotNil(t, result) 202 | 203 | assert.Equal(t, in2, result.String()) 204 | assert.Empty(t, result.GetPointer().String()) 205 | } 206 | 207 | func TestFileScheme(t *testing.T) { 208 | const ( 209 | in1 = "file:///Users/mac/1.json#a" 210 | in2 = "file:///Users/mac/2.json#b" 211 | ) 212 | 213 | r1, err := New(in1) 214 | require.NoError(t, err) 215 | r2, err := New(in2) 216 | require.NoError(t, err) 217 | 218 | require.False(t, r1.HasFragmentOnly) 219 | require.True(t, r1.HasFileScheme) 220 | require.True(t, r1.HasFullFilePath) 221 | require.True(t, r1.IsCanonical()) 222 | assert.Empty(t, r1.GetPointer().String()) 223 | 224 | result, err := r1.Inherits(r2) 225 | require.NoError(t, err) 226 | assert.Equal(t, in2, result.String()) 227 | assert.Empty(t, result.GetPointer().String()) 228 | } 229 | 230 | func TestReferenceResolution(t *testing.T) { 231 | // 5.4. Reference Resolution Examples 232 | // http://tools.ietf.org/html/rfc3986#section-5.4 233 | const base = "http://a/b/c/d;p?q" 234 | 235 | baseRef, err := New(base) 236 | require.NoError(t, err) 237 | require.Equal(t, base, baseRef.String()) 238 | 239 | checks := []string{ 240 | // 5.4.1. Normal Examples 241 | // http://tools.ietf.org/html/rfc3986#section-5.4.1 242 | 243 | "g:h", "g:h", 244 | "g", "http://a/b/c/g", 245 | "./g", "http://a/b/c/g", 246 | "g/", "http://a/b/c/g/", 247 | "/g", "http://a/g", 248 | "//g", "http://g", 249 | "?y", "http://a/b/c/d;p?y", 250 | "g?y", "http://a/b/c/g?y", 251 | "#s", "http://a/b/c/d;p?q#s", 252 | "g#s", "http://a/b/c/g#s", 253 | "g?y#s", "http://a/b/c/g?y#s", 254 | ";x", "http://a/b/c/;x", 255 | "g;x", "http://a/b/c/g;x", 256 | "g;x?y#s", "http://a/b/c/g;x?y#s", 257 | "", "http://a/b/c/d;p?q", 258 | ".", "http://a/b/c/", 259 | "./", "http://a/b/c/", 260 | "..", "http://a/b/", 261 | "../", "http://a/b/", 262 | "../g", "http://a/b/g", 263 | "../..", "http://a/", 264 | "../../", "http://a/", 265 | "../../g", "http://a/g", 266 | 267 | // 5.4.2. Abnormal Examples 268 | // http://tools.ietf.org/html/rfc3986#section-5.4.2 269 | 270 | "../../../g", "http://a/g", 271 | "../../../../g", "http://a/g", 272 | 273 | "/./g", "http://a/g", 274 | "/../g", "http://a/g", 275 | "g.", "http://a/b/c/g.", 276 | ".g", "http://a/b/c/.g", 277 | "g..", "http://a/b/c/g..", 278 | "..g", "http://a/b/c/..g", 279 | 280 | "./../g", "http://a/b/g", 281 | "./g/.", "http://a/b/c/g/", 282 | "g/./h", "http://a/b/c/g/h", 283 | "g/../h", "http://a/b/c/h", 284 | "g;x=1/./y", "http://a/b/c/g;x=1/y", 285 | "g;x=1/../y", "http://a/b/c/y", 286 | 287 | "g?y/./x", "http://a/b/c/g?y/./x", 288 | "g?y/../x", "http://a/b/c/g?y/../x", 289 | "g#s/./x", "http://a/b/c/g#s/./x", 290 | "g#s/../x", "http://a/b/c/g#s/../x", 291 | 292 | "http:g", "http:g", // for strict parsers 293 | // "http:g", "http://a/b/c/g", // for backward compatibility 294 | 295 | } 296 | for i := 0; i < len(checks); i += 2 { 297 | child := checks[i] 298 | expected := checks[i+1] 299 | 300 | childRef, e := New(child) 301 | require.NoErrorf(t, e, "test: %d: New(%s) failed error: %v", i/2, child, e) 302 | 303 | res, e := baseRef.Inherits(childRef) 304 | require.NoErrorf(t, e, "test: %d", i/2) 305 | require.NotNilf(t, res, "test: %d", i/2) 306 | assert.Equalf(t, expected, res.String(), "test: %d", i/2) 307 | } 308 | } 309 | 310 | func TestIdenticalURLEncoded(t *testing.T) { 311 | expected, err := New("https://localhost/🌭#/🍔") 312 | require.NoErrorf(t, err, "failed to create jsonreference: %v", err) 313 | 314 | actual, err := New("https://localhost/%F0%9F%8C%AD#/%F0%9F%8D%94") 315 | require.NoErrorf(t, err, "failed to create jsonreference: %v", err) 316 | require.Equal(t, expected, actual) 317 | } 318 | --------------------------------------------------------------------------------