|
1 | 1 | # Contributing
|
2 | 2 |
|
3 |
| -Thanks for being willing to contribute! |
| 3 | +Thanks for being willing to contribute! 🙏 |
4 | 4 |
|
5 |
| -**Working on your first Pull Request?** You can learn how from this _free_ series [How to Contribute to an Open Source |
6 |
| -Project on GitHub][egghead] |
| 5 | +**Working on your first Pull Request (PR)?** You can learn how from this _free_ series [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github) |
7 | 6 |
|
8 |
| -## Project setup |
| 7 | +## Open issues |
| 8 | + |
| 9 | +Please check out the [the open issues](https://github.com/drwpow/openapi-typescript/issues). Issues labelled [**Help Wanted**](https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [**Good First Issue**](https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) are especially good to help with. |
| 10 | + |
| 11 | +Contributing doesn’t have to be in code! Simply answering questions in open issues, or providing workarounds, is just as important a contribution as making pull requests. |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +### Dependencies |
9 | 16 |
|
10 | 17 | 1. Install [pnpm](https://pnpm.io/)
|
11 | 18 | 2. Fork and clone the repo
|
12 | 19 | 3. Run `pnpm i` to install dependencies
|
13 | 20 | 4. Create a branch for your PR with `git checkout -b pr/your-branch-name`
|
14 | 21 |
|
15 |
| -It’s also recommended you have [ESLint][eslint] installed and set up correctly. You may also run the command |
16 |
| -`npm run lint` to see lint errors. |
| 22 | +### VS Code setup |
17 | 23 |
|
18 |
| -> Tip: Keep your `main` branch pointing at the original repository and make pull requests from branches on your fork. To |
19 |
| -> do this, run: |
20 |
| -> |
21 |
| -> ``` |
22 |
| -> git remote add upstream https://github.com/drwpow/openapi-typescript.git |
23 |
| -> git fetch upstream |
24 |
| -> git branch --set-upstream-to=upstream/main main |
25 |
| -> ``` |
26 |
| -> |
27 |
| -> This will add the original repository as a "remote" called "upstream," Then fetch the git information from that |
28 |
| -> remote, then set your local `main` branch to use the upstream main branch whenever you run `git pull`. Then you can |
29 |
| -> make all of your pull request branches based on this `main` branch. Whenever you want to update your version of |
30 |
| -> `main`, do a regular `git pull`. |
| 24 | +If using VS Code, the following extensions are recommended (or their equivalent extensions if using another editor) |
31 | 25 |
|
32 |
| -## Committing and pushing changes |
| 26 | +- [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) |
33 | 27 |
|
34 |
| -Please make sure to run the tests (`npm test`) before you commit your changes. |
| 28 | +## Workflow |
35 | 29 |
|
36 |
| -### Local schemas |
| 30 | +When working locally, run: |
37 | 31 |
|
38 |
| -This repo supports OpenAPI 2.0 and 3.0. There are some real-world examples located in `tests/v2/specs/*.yaml` and |
39 |
| -`tests/v3/specs/*.yaml`, respectively. Testing large, real schemas was a major goal of this project. Many libraries only |
40 |
| -test the “Petstore” example from Swagger, which is contrived and is missing much complexity from companies’ production |
41 |
| -schemas. |
| 32 | +```bash |
| 33 | +npm run dev |
| 34 | +``` |
42 | 35 |
|
43 |
| -_Note: don’t update the `yaml` schemas with your own custom additions (but if the official versions have updated, then |
44 |
| -it’s fine to update them here). If you’d like to add your schema for testing, then please add them as a new schema, and |
45 |
| -add them to `./expected/*.ts`._ |
| 36 | +This will compile the code as you change automatically. |
46 | 37 |
|
47 |
| -#### Regenerating schemas |
| 38 | +### TDD |
48 | 39 |
|
49 |
| -If you’ve added a feature or fixed a bug and need to update the generated schemas, run the following: |
| 40 | +This library is a great usecase for [test-driven development (TDD)](https://en.wikipedia.org/wiki/Test-driven_development). If you’re new to this, the basic workflow is: |
50 | 41 |
|
51 |
| -``` |
52 |
| -# 1. re-build the package |
53 |
| -npm run build |
54 |
| -# 2. run the local CLI (not the npm one!) |
55 |
| -./bin/cli.js tests/v3/specs/github.yaml -o tests/v3/expected/github.ts |
56 |
| -# NOTE: on Windows, try running the script on WSL if getting errors |
57 |
| -``` |
| 42 | +1. First, write a [test](#testing) that fully outlines what you’d _like_ the output to be. |
| 43 | +2. Make sure this test **fails** when you run `npm test` (yes, _fails!_) |
| 44 | +3. Then, make changes to `src/` until the tests pass. |
| 45 | + |
| 46 | +_Code generation is hard!_ And for that reason, starting with a very clear expectation of your end-goal can make working easier. |
| 47 | + |
| 48 | +### Unit tests or snapshot tests? |
| 49 | + |
| 50 | +This library has both unit tests (tests that test a tiny part of a schema) and snapshot tests (tests that run over an entire, complete schema). When opening a PR, the former are more valuable than the latter, and are always required. However, updating snapshot tests can help with the following: |
| 51 | + |
| 52 | +- Fixing bugs that deal with multiple schemas with remote `$ref`s |
| 53 | +- Fixing Node.js or OS-related bugs |
| 54 | +- Adding a CLI option that changes the entire output |
| 55 | + |
| 56 | +For most PRs, **snapshot tests can be avoided.** But for scenarios similar to the ones mentioned, they can ensure everything is working as expected. |
| 57 | + |
| 58 | +### Generating types |
| 59 | + |
| 60 | +It may be surprising to hear, but _generating TypeScript types from OpenAPI is opinionated!_ Even though TypeScript and OpenAPI are very close relatives, both being JavaScript/JSON-based, they are nonetheless 2 different languages and thus there is always some room for interpretation. Likewise, some parts of the OpenAPI specification can be ambiguous on how they’re used, and what the expected type outcomes may be (though this is generally for more advanced usecasees, such as specific implementations of `anyOf` as well as [discriminator](https://spec.openapis.org/oas/latest.html#discriminatorObject) and complex polymorphism). |
58 | 61 |
|
59 |
| -This should update the expected TypeScript definiton. |
| 62 | +All that said, this library should strive to generate _the most predictable_ TypeScript output for a given schema. And to achieve that, it always helps to open an [issue](https://github.com/drwpow/openapi-typescript/issues) or [discussion](https://github.com/drwpow/openapi-typescript/discussions) to gather feedback. |
60 | 63 |
|
61 |
| -_Also if this appears in `examples/` feel free to update that, too!_ |
| 64 | +### Opening a PR |
| 65 | + |
| 66 | +When opening a pull request, make sure all of the following is done: |
| 67 | + |
| 68 | +- [x] Tests are added |
| 69 | +- [x] Build passes (`npm run build`) |
| 70 | +- [x] Tests pass (`npm test`) |
| 71 | +- [x] Linting passes (`npm run lint`) |
| 72 | + |
| 73 | +Lastly, be sure to fill out the complete PR template! |
62 | 74 |
|
63 | 75 | ## Testing
|
64 | 76 |
|
65 |
| -Tests use [Vitest](https://vitest.dev), a modern test-runner based on Jest. To run the tests locally, run: |
| 77 | +This library uses [Vitest](https://vitest.dev/) for testing. There’s a great [VS Code extension](https://marketplace.visualstudio.com/items?itemName=ZixuanChen.vitest-explorer) you can optionally use if you’d like in-editor debugging tools. |
66 | 78 |
|
67 |
| -``` |
| 79 | +### Running tests |
| 80 | + |
| 81 | +💡 The tests test **the production build** in `dist/`. Be sure to run `npm run build` before running tests (or keep `npm run dev` running in the background, which compiles as-you-work)! |
| 82 | + |
| 83 | +To run the entire test suite once, run: |
| 84 | + |
| 85 | + |
| 86 | +```bash |
68 | 87 | npm test
|
69 | 88 | ```
|
70 | 89 |
|
71 | 90 | To run an individual test:
|
72 | 91 |
|
73 |
| -``` |
74 |
| -npx vitest [part of filename] |
| 92 | +```bash |
| 93 | +npx vitest [partial filename] |
75 | 94 | ```
|
76 | 95 |
|
77 |
| -Or to start all tests in watch mode: |
| 96 | +To start the entire test suite in watch mode: |
78 | 97 |
|
79 |
| -``` |
| 98 | +```bash |
80 | 99 | npx vitest
|
81 | 100 | ```
|
82 | 101 |
|
83 |
| -[See docs](https://vitest.dev) |
| 102 | +### Running linting |
84 | 103 |
|
85 |
| -## Help needed |
| 104 | +To run ESLint on the project: |
86 | 105 |
|
87 |
| -Please check out the [the open issues][issues]. Issues labelled [**Help Wanted**][help-wanted] and [**Good First |
88 |
| -Issue**][good-first-issue] are especially good to help with. |
89 |
| -
|
90 |
| -Also, please watch the repo and respond to questions/bug reports/feature requests! Thanks! |
91 |
| -
|
92 |
| -[all-contributors]: https://github.com/all-contributors/all-contributors |
93 |
| -[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github |
94 |
| -[eslint]: https://eslint.org/ |
95 |
| -[good-first-issue]: |
96 |
| - https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22 |
97 |
| -[help-wanted]: https://github.com/drwpow/openapi-typescript/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22 |
98 |
| -[issues]: https://github.com/drwpow/openapi-typescript/issues |
| 106 | +```bash |
| 107 | +npm run lint |
| 108 | +``` |
0 commit comments