Commit cf181c2
correctly resolve references to a type that is itself just a single allOf reference (#1103)
Fixes #1091
Example of a valid spec that triggered this bug:
https://gist.github.com/eli-bl/8f5c7d1d872d9fda5379fa6370dab6a8
In this spec, CreateCat contains only an `allOf` with a single reference
to CreateAnimal. The current behavior of `openapi-python-client` in such
a case is that it treats CreateCat as simply an alias for CreateAnimal;
any references to it are treated as references to CreateAnimal, and it
doesn't bother creating a model class for CreateCat. And if the spec
contained only those two types, then this would be successful.
(Note, the term "alias" doesn't exist in OpenAPI/JSON Schema, but I'm
using it here to mean "a type that extends one other type with `allOf`,
with no changes." Whether that should be treated as a separate thing in
any way is not really a concern of OpenAPI; it's an issue for us only
because we are generating code for model classes. See also:
#1104)
Anyway, in this case the spec also contains UpdateCat, which extends
CreateCat with an additional property. This _should_ be exactly the same
as extending CreateAnimal... but, prior to this fix, it resulted in a
parsing error. The problem happened like this:
1. In `_create_schemas`, we create a ModelProperty for each of the three
schemas.
* The one for CreateCat is handled slightly differently: its `data`
attribute points to the exact same schema as CreateAnimal, and we do not
add it into `schemas.classes_by_name` because we don't want to generate
a separate Python class for it.
2. In `_process_models`, we're attempting to finish filling in the
property list for each model.
* That might not be possible right away because there might be a
reference to another model that hasn't been fully processed yet. So we
iterate as many times as necessary until they're all fully resolved.
However...
* What we are iterating over is `schemas.classes_by_name`. There's an
incorrect assumption that every named model is included in that dict; in
this case, CreateCat is not in it.
* Therefore, CreateCat remains in an invalid state, and the reference
from CreateAnimal to CreateCat causes an error.
My solution is to use `classes_by_name` only for the purpose of
determining what Python classes to generate, and add a new collection,
`models_to_process`, which includes _every_ ModelProperty including ones
that are aliases.
After the fix, generating a client from the example spec succeeds. The
only Python model classes created are CreateAnimal and UpdateCat; the
`post` endpoint that referenced CreateCat uses the CreateAnimal class.
Again, that's consistent with how `openapi-python-client` currently
handles these type aliases; the difference is just that it no longer
fails when it sees a reference _to_ the alias.
---------
Co-authored-by: Dylan Anthony <dbanty@users.noreply.github.com>1 parent 5f7c9a5 commit cf181c2
File tree
14 files changed
+932
-59
lines changed- .changeset
- end_to_end_tests
- custom-templates-golden-record/my_test_api_client/api/default
- golden-record/my_test_api_client
- api/default
- models
- openapi_python_client/parser
- properties
- tests/test_parser/test_properties
14 files changed
+932
-59
lines changedLines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1629 | 1629 | | |
1630 | 1630 | | |
1631 | 1631 | | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
| 1650 | + | |
| 1651 | + | |
| 1652 | + | |
| 1653 | + | |
| 1654 | + | |
| 1655 | + | |
| 1656 | + | |
| 1657 | + | |
| 1658 | + | |
1632 | 1659 | | |
1633 | 1660 | | |
1634 | 1661 | | |
| |||
1647 | 1674 | | |
1648 | 1675 | | |
1649 | 1676 | | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
1650 | 1694 | | |
1651 | 1695 | | |
1652 | 1696 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1619 | 1619 | | |
1620 | 1620 | | |
1621 | 1621 | | |
1622 | | - | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
1623 | 1650 | | |
1624 | 1651 | | |
1625 | 1652 | | |
| |||
1637 | 1664 | | |
1638 | 1665 | | |
1639 | 1666 | | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
1640 | 1684 | | |
1641 | 1685 | | |
1642 | 1686 | | |
| |||
1667 | 1711 | | |
1668 | 1712 | | |
1669 | 1713 | | |
1670 | | - | |
1671 | | - | |
1672 | | - | |
1673 | | - | |
1674 | | - | |
| 1714 | + | |
1675 | 1715 | | |
1676 | 1716 | | |
1677 | 1717 | | |
| |||
1808 | 1848 | | |
1809 | 1849 | | |
1810 | 1850 | | |
1811 | | - | |
1812 | | - | |
1813 | | - | |
1814 | | - | |
1815 | | - | |
| 1851 | + | |
1816 | 1852 | | |
1817 | 1853 | | |
1818 | 1854 | | |
| |||
1825 | 1861 | | |
1826 | 1862 | | |
1827 | 1863 | | |
1828 | | - | |
1829 | | - | |
1830 | | - | |
1831 | | - | |
1832 | | - | |
| 1864 | + | |
1833 | 1865 | | |
1834 | 1866 | | |
1835 | 1867 | | |
| |||
Lines changed: 5 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
Lines changed: 122 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| 116 | + | |
114 | 117 | | |
115 | 118 | | |
116 | 119 | | |
| 120 | + | |
117 | 121 | | |
118 | 122 | | |
119 | 123 | | |
| |||
0 commit comments