Skip to content

Don't depend on stringcase (closes #369) #375

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
merged 7 commits into from
Apr 28, 2021

Conversation

ramnes
Copy link
Contributor

@ramnes ramnes commented Apr 1, 2021

Let's reimplement what we need here so that we don't depend on stringcase
anymore (see #369 for rationale). I've added a few test cases for stuff I've
seen break along the way.

Let's reimplement what we need here so that we don't depend on stringcase
anymore (see openapi-generators#369 for rationale). I've added a few test cases for stuff I've
seen break along the way.
@ramnes
Copy link
Contributor Author

ramnes commented Apr 1, 2021

This kind of work with regexps and a large variety of use cases is always a bit fragile, so even if all the tests pass, I'd recommend that you test the branch against other openapi.yaml files you may have already a generated client to see if there isn't any weird diff. Only fixed a lot of bugs on my side. :)

@dbanty
Copy link
Collaborator

dbanty commented Apr 1, 2021

Is there a rationale for implementing our own vs relying on one of the other libraries you mentioned in #369? I'm thinking one of them might have a more expansive test suite to catch any corner-case bugs (and if they don't... they should 😂)

@ramnes
Copy link
Contributor Author

ramnes commented Apr 1, 2021

Yeah, none of the two libraries passed the tests out of the box. Both needed hacks or pull-requests to make everything work together. I figured out that rather than spending time to write hacks or modify yet another project, I'd rather implement that here. It's just three functions to maintain, so I don't think it's worth a dependency, and most importantly, that would give you full control over the generated output.

About full control... The e2e tests are broken as you can see, and this is due to what seems to me to be an inconsistency in the previous implementation. :)

What do you think of such diffs in the generated output?

-from ...models.test_inline_objects_response_200 import TestInlineObjectsResponse_200
+from ...models.test_inline_objects_response_200 import TestInlineObjectsResponse200
-from .model_with_union_property_inlined_fruit_type0 import ModelWithUnionPropertyInlinedFruitType0
-from .model_with_union_property_inlined_fruit_type1 import ModelWithUnionPropertyInlinedFruitType1
+from .model_with_union_property_inlined_fruit_type_0 import ModelWithUnionPropertyInlinedFruitType0
+from .model_with_union_property_inlined_fruit_type_1 import ModelWithUnionPropertyInlinedFruitType1

@ramnes
Copy link
Contributor Author

ramnes commented Apr 1, 2021

Here are two diffs that would make things more consistent: https://gist.github.com/ramnes/611b874b878f55f5bfc4b781d48c40a1

current.diff is the diff generated by the current state of the PR, and next.diff is the diff generated by a one line diff I have not pushed yet. In my opinion next.diff looks much better, but it has a higher chance of introducing breaking changes for people that already have a generated client and want to update the generator.

@ramnes
Copy link
Contributor Author

ramnes commented Apr 1, 2021

FWIW, here is the diff when running the current version of the PR against sftpgo-client: ramnes/sftpgo-client@8e620d5

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

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

A couple tweaks to make, and I think I'm going to try and preserve the previous behavior of saving leading delimiters just to reduce update friction.

return fix_keywords(stringcase.pascalcase(sanitize(value.replace(" ", ""))))
words = split_words(sanitize(value))
words = [word.capitalize() if not word.isupper() else word for word in words]
value = "".join(words)
Copy link
Collaborator

Choose a reason for hiding this comment

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

A side-effect of this is that a leading delimiter gets stripped. So _MyClass and __MyClass both become MyClass. This may not be a problem, but it is a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I wonder if we should stay consistent or not. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

After thinking about it a bit, I think it's better to remove the leading underscores since that has the semantics in Python of being private. Nothing in the generated interfaces should be private so... this new behavior seems better.

@dbanty dbanty linked an issue Apr 25, 2021 that may be closed by this pull request
@dbanty dbanty added this to the 0.9.0 milestone Apr 25, 2021
@codecov
Copy link

codecov bot commented Apr 25, 2021

Codecov Report

Merging #375 (000c3d3) into main (83a360f) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #375   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           46        46           
  Lines         1523      1530    +7     
=========================================
+ Hits          1523      1530    +7     
Impacted Files Coverage Δ
openapi_python_client/utils.py 100.00% <100.00%> (ø)

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 83a360f...000c3d3. Read the comment docs.

@@ -27,5 +27,5 @@
from .model_with_union_property_inlined_fruit_type0 import ModelWithUnionPropertyInlinedFruitType0
from .model_with_union_property_inlined_fruit_type1 import ModelWithUnionPropertyInlinedFruitType1
from .test_inline_objects_json_body import TestInlineObjectsJsonBody
from .test_inline_objects_response_200 import TestInlineObjectsResponse_200
from .test_inline_objects_response200 import TestInlineObjectsResponse200
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another issue 😞. I imagine this is why all of the existing string case libraries get it wrong... so many edge cases. I'll add a unit test for this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's exactly what I was talking about in my previous comment.

Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

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

Alright, this covers all the cases I can think of now. @ramnes will you take a look through the changes when you get a chance and make sure it looks good to you?

@ramnes
Copy link
Contributor Author

ramnes commented Apr 26, 2021

I think there are a few problems but I can't dig right now, please don't merge it yet.

@dbanty dbanty removed this from the 0.9.0 milestone Apr 26, 2021
@ramnes
Copy link
Contributor Author

ramnes commented Apr 28, 2021

Alright, so #397 is the problem I saw while trying this branch in real conditions. I thought it was linked to the changes here and it's actually not, but it makes it tricky for me to smoke test this branch due to the lot of noise it introduces.

@dbanty
Copy link
Collaborator

dbanty commented Apr 28, 2021

Awesome, thanks for checking it out! I'm going to move forward with this one then and we can squash that bug separately.

@dbanty dbanty added this to the 0.9.0 milestone Apr 28, 2021
@dbanty dbanty enabled auto-merge (squash) April 28, 2021 14:41
@dbanty dbanty merged commit 85a070a into openapi-generators:main Apr 28, 2021
@ramnes
Copy link
Contributor Author

ramnes commented May 3, 2021

I think that one of your commits here introduced two problems:

-            id_=id_,
+            id=id,

and

-    s3config: Union[Unset, S3Config] = UNSET
+    s_3_config: Union[Unset, S3Config] = UNSET

@dbanty
Copy link
Collaborator

dbanty commented May 3, 2021

id=id was caused by #407 and is intentional.

The s_3_config thing is unfortunate but intentional... I wanted to make numbers be separate words since we often have things like Response200 which should, in my opinion, be in a module called response_200. I think S3 is a weird case and that if we have to pick one then we should opt for numbers be separate words, not part of larger words. Do you disagree?

@ramnes
Copy link
Contributor Author

ramnes commented May 4, 2021

Maybe it should only be a separate word if it doesn't follow a capital letter?

@ramnes ramnes mentioned this pull request May 6, 2021
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.

The stringcase library is buggy and unmaintained
2 participants