Skip to content
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

fix!: Change reference resolution to use reference path instead of class name (fixes #342) #366

Conversation

dbanty
Copy link
Collaborator

@dbanty dbanty commented Mar 27, 2021

This one touches a lot of code and has multiple breaking changes:

  1. When looking up a reference, it will be found via reference path (e.g. #/components/schemas/whatever) instead of the generated class name. This fixes broken behavior where we were failing to find the right references sometimes but also could change generated clients unexpectedly on people.
  2. Config was reworked to rely on fewer globals (there's still at least one 😢). The --config param should be passed in after the command (e.g. generate) now instead of before it. Class name and module name overrides may apply differently than they used to but are hopefully more deterministic now.

@codecov
Copy link

codecov bot commented Apr 4, 2021

Codecov Report

Merging #366 (e131d13) into main (bf575fb) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #366   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           47        46    -1     
  Lines         1490      1508   +18     
=========================================
+ Hits          1490      1508   +18     
Impacted Files Coverage Δ
openapi_python_client/__init__.py 100.00% <100.00%> (ø)
openapi_python_client/cli.py 100.00% <100.00%> (ø)
openapi_python_client/config.py 100.00% <100.00%> (ø)
openapi_python_client/parser/__init__.py 100.00% <100.00%> (ø)
openapi_python_client/parser/errors.py 100.00% <100.00%> (ø)
openapi_python_client/parser/openapi.py 100.00% <100.00%> (ø)
...penapi_python_client/parser/properties/__init__.py 100.00% <100.00%> (ø)
...i_python_client/parser/properties/enum_property.py 100.00% <100.00%> (ø)
..._python_client/parser/properties/model_property.py 100.00% <100.00%> (ø)
openapi_python_client/parser/properties/schemas.py 100.00% <100.00%> (ø)
... and 1 more

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 bf575fb...e131d13. Read the comment docs.

@dbanty dbanty marked this pull request as ready for review April 4, 2021 17:25
@dbanty dbanty requested a review from emann April 4, 2021 17:25
@dbanty
Copy link
Collaborator Author

dbanty commented Apr 4, 2021

I think this is finally ready for review! @dongfangtianyu originally reported the error, so if you could do a review pass (or at least test the new code with your OpenAPI documents) that would be super helpful. As always, I'd also like @packyg / @dtkav / @forest-benchling to take a look as well since they have the largest collection of documents and generated clients that I know of (and since this will probably mess with their fork).

@dbanty dbanty added this to the 0.9.0 milestone Apr 4, 2021
@dbanty
Copy link
Collaborator Author

dbanty commented Apr 4, 2021

Oh and also @p1-ra and @p1-alexandrevaney are probably interested since you have a few other PRs open related to references.

Copy link
Collaborator

@forest-benchling forest-benchling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cc-ing us. This looks like a good change and I don't think it will break our use case. We don't use title on any models that are referenced by other models, and we don't use any config in generation.

cc @packyg @dbanty @bowenwr

openapi_python_client/config.py Show resolved Hide resolved

@attr.s(auto_attribs=True, frozen=True)
class Class:
""" Info about a generated class which will be in models """
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you mean here, but this docstring is a little confusingly worded

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a suggestion? Maybe "Info about a class to be generated"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Represents Python class which will be generated from OpenAPI schema"?

@dongfangtianyu
Copy link
Contributor

It looks great !
I have tested my OpenAPI file and everything is OK.

emann
emann previously approved these changes Apr 7, 2021
Copy link
Collaborator

@emann emann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - @p1-ra I'd be interested to hear if this fixes both issues you were trying to solve in #329. I would also imagine adding support for remote references will be far easier now.

…s,-when-components-title-is-different-from-the-name
@dbanty dbanty requested a review from emann April 7, 2021 13:51
@dbanty dbanty merged commit b719316 into main Apr 12, 2021
@dbanty dbanty deleted the 342-could-not-find-reference-in-parsed-models,-when-components-title-is-different-from-the-name branch April 12, 2021 14:51
@p1-ra
Copy link
Contributor

p1-ra commented May 5, 2021

Kind of old, but I've missed the notification. As for information, regarding #329 it almost fixes them all (tested on today main branch):

  • Model property indirect reference: OK
openapi: 3.0.2
info:
  title: Model property Indirect reference generation test
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithIndirectReferenceProperty:
      title: AModelWithIndirectReferenceProperty
      type: object
      properties:
        an_enum_indirect_ref:
          ref: "#/components/schemas/AnEnumIndirectReference"
    AnEnumIndirectReference:
      ref: "#/components/schemas/AnEnum"
    AnEnum:
      title: "AnEnum"
      enum:
        - FIRST_VALUE
        - SECOND_VALUE
  • Model property deeper indirect reference: OK
openapi: 3.0.2
info:
  title: Model property deeper Indirect reference generation test
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithIndirectReferenceProperty:
      title: AModelWithIndirectReferenceProperty
      type: object
      properties:
        an_enum_deep_indirect_ref:
          ref: "#/components/schemas/AnEnumDeeperIndirectReference"
    AnEnumDeeperIndirectReference:
      ref: "#/components/schemas/AnEnumIndirectReference"
    AnEnumIndirectReference:
      ref: "#/components/schemas/AnEnum"
    AnEnum:
      title: "AnEnum"
      enum:
        - FIRST_VALUE
        - SECOND_VALUE
  • Model property self reference: OK
openapi: 3.0.2
info:
  title: Model property self reference generation test
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithSelfReferenceProperty:
      title: AModelWithIndirectReferenceProperty
      type: object
      properties:
        self_ref:
          ref: "#/components/schemas/AModelWithSelfReferenceProperty"
  • Model property self indirect reference: NOK
openapi: 3.0.2
info:
  title: Model property self indirect reference generation test
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithSelfReferenceProperty:
      title: AModelWithIndirectReferenceProperty
      type: object
      properties:
        self_ref:
          ref: "#/components/schemas/AModelWithSelfReferencePropertyIndirectRef"
    AModelWithSelfReferencePropertyIndirectRef:
      ref: "#/components/schemas/AModelWithSelfReferenceProperty"
  • Model property self deeper indirect reference: NOK
openapi: 3.0.2
info:
  title: Model property self indirect reference generation test
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithSelfReferenceProperty:
      title: AModelWithIndirectReferenceProperty
      type: object
      properties:
        self_ref:
          ref: "#/components/schemas/AModelWithSelfReferencePropertyDeeperIndirectRef"
    AModelWithSelfReferencePropertyDeeperIndirectRef:
      ref: "#/components/schemas/AModelWithSelfReferencePropertyIndirectRef"
    AModelWithSelfReferencePropertyIndirectRef:
      ref: "#/components/schemas/AModelWithSelfReferenceProperty"

@emann @dbanty

@p1-ra
Copy link
Contributor

p1-ra commented May 5, 2021

By removing the title attribute of the two failure specs (who was invalid title attribute I thinks), it works as expected:

  • Model property self indirect reference: OK
openapi: 3.0.2
info:
  title: Model property self indirect reference generation test v2 -- no title
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithSelfReferenceProperty:
      type: object
      properties:
        self_ref:
          ref: "#/components/schemas/AModelWithSelfReferencePropertyIndirectRef"
    AModelWithSelfReferencePropertyIndirectRef:
      ref: "#/components/schemas/AModelWithSelfReferenceProperty"
  • Model property self` deeper indirect reference: OK
openapi: 3.0.2
info:
  title: Model property self indirect reference generation test v2 -- no title
  description: One bug to solves them all
  version: 9001

paths: {}

components:
  schemas:
    AModelWithSelfReferenceProperty:
      type: object
      properties:
        self_ref:
          ref: "#/components/schemas/AModelWithSelfReferencePropertyDeeperIndirectRef"
    AModelWithSelfReferencePropertyDeeperIndirectRef:
      ref: "#/components/schemas/AModelWithSelfReferencePropertyIndirectRef"
    AModelWithSelfReferencePropertyIndirectRef:
      ref: "#/components/schemas/AModelWithSelfReferenceProperty"

@emann
Copy link
Collaborator

emann commented May 5, 2021

Thanks for getting back to us @p1-ra! Glad to hear its working :)

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.

None yet

5 participants