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: rust compile errors #869

Merged
merged 22 commits into from
Nov 16, 2022

Conversation

YushiOMOTE
Copy link
Contributor

@YushiOMOTE YushiOMOTE commented Sep 3, 2022

Description

Locally tried Rust generator with more complicated private examples and got some compile errors in the generate Rust crate.
The PR makes compile pass fixing the following issues:

  • Remove warnings in case the model name contains the dash + number (e.g. Abc_123)
  • Compile errors due to mismatch of some Option field definitions and constructors in struct
  • Fix broken generated file names
  • Fix invalid type name included in generated Union types
  • Fix the missing qualifer crate:: in some cases
  • Fix undefined error when schemaUid doesn't exist

The normal toPascalCase method separates number e.g.
`abc_123` is converted to `Abc_123`. This causes
compile warnings in Rust. The new method converts it to
`Abc123`.
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@YushiOMOTE YushiOMOTE changed the title Fix Rust compile errors fix: Rust compile errors Sep 3, 2022
@YushiOMOTE YushiOMOTE changed the title fix: Rust compile errors fix: rust compile errors Sep 3, 2022
@YushiOMOTE YushiOMOTE marked this pull request as ready for review September 3, 2022 08:26
@leigh-johnson
Copy link
Collaborator

👋 Hey @YushiOMOTE, thank you for the contributions! Would you feel comfortable adding tests to demonstrate how the code breaks, and verify your fixes? Feel free to ping me if you're having trouble getting the tests (unit or black-box) running locally.

An excellent first step would be to add a test for renderMacro revisions to: https://github.com/asyncapi/modelina/blob/next/test/generators/rust/RustGenerator.spec.ts

Can you share a schema with example data structures? That will make it easier to grok the behavior you're describing. Thanks!

The ConstrainedUnionModel isn't used because we use ConstrainedEnumModel to describe an enum of primitive type(s) and mixed/complex types ("union" in most languages).

Regarding imports, the decision to render unit types and use fully-qualified crate:: syntax was intentional. We follow this convention in the openapi-generator for data structures and fn signatures.
https://github.com/OpenAPITools/openapi-generator/blob/60dcf8613f6b75b606757c4a991fe018ad10ee99/samples/client/petstore/rust/reqwest/petstore/src/models/pet.rs#L26
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/rust/reqwest/petstore/src/apis/pet_api.rs#L86

@leigh-johnson
Copy link
Collaborator

link against #847 for ref

@YushiOMOTE
Copy link
Contributor Author

Thank you! Yes let me add tests. I cannot share the exact schema as it’s closed, but probably can share example schemas that can reproduce the issues.

@YushiOMOTE YushiOMOTE force-pushed the fix-rust-issues branch 2 times, most recently from 51e1ca3 to 1ab80a6 Compare September 6, 2022 11:19
There was mismatch of conditions for struct constructor and
definition.
`modelName` contains `CargoToml` or `lib` instead of `Cargo.toml`
and `lib.rs`. Fixed to use `model.name` that contains the actual file names.
The `type` field of union contains the type name with suffix number,
which is non-existent type name. The actual name is stored in
`ref` side.
Follow the actual Rust spec to generate PartialEq, PartialOrd, Eq, Ord
derives.
@@ -50,7 +50,7 @@ impl Address {
exports[`Should be able to render Rust Models and should log expected output to console 2`] = `
Array [
"// Members represents a union of types: String, f64, bool
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Expected as f64 supports PartialOrd

@YushiOMOTE
Copy link
Contributor Author

Updated/added tests. npm test passes locally

Test Suites: 148 passed, 148 total
Tests:       3 skipped, 977 passed, 980 total
Snapshots:   199 passed, 199 total
Time:        40.065 s
Ran all test suites.

Regarding imports, the decision to render unit types and use fully-qualified crate:: syntax was intentional. We follow this convention in the openapi-generator for data structures and fn signatures.

I have changed to use fully-qualified syntax instead of importing.

Can you share a schema with example data structures? That will make it easier to grok the behavior you're describing.

I think the updated snapshots in this PR describes most of the issues I faced. Except for this one:

Fix invalid type name included in generated Union types (78c44a2)

I'm trying to give the example swagger or add a test in PR to describe this case soon 👀

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum Things {
pub enum Things123 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Things_123 in schema is converted to Things123 now. Previously, it was Things_123 which caused compile warnings.

@@ -2,18 +2,18 @@

exports[`RUST_COMMON_PRESET Enum should render \`enum\` of union type with Default implementation of integer type 1`] = `
"// Address represents a Address model.
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ord and PartialOrd are removed now as HashMap doesn't support those.

pub struct Address {
#[serde(rename=\\"members\\")]
members: Box<crate::Members>,
members: crate::Members,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Expected. Since we take Members in fn new side without Box, the definition should be just Members as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should keep references to models as pointer-heap memory by default. If you need the entire data structure to be loaded into stack memory, I think a preset would be the right mechanism for that behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! I fixed to use Box for reference. Now the constructor side is fixed to use Box: https://github.com/asyncapi/modelina/pull/869/files#diff-9f4739c9c2692512b77fc209d671487d26937caed76f432ff6adf62a343a3ebaR18

#[serde(rename=\\"optional_members\\", skip_serializing_if = \\"Option::is_none\\")]
optional_members: Option<Box<crate::OptionalMembers>>,
#[serde(rename=\\"additionalProperties\\", skip_serializing_if = \\"Option::is_none\\")]
additional_properties: Option<std::collections::HashMap<String, String>>,
}

impl Address {
pub fn new(members: Members, optional_members: Option<crate::OptionalMembers>, additional_properties: Option<std::collections::HashMap<String, String>>) -> Address {
pub fn new(members: crate::Members, optional_members: Option<crate::OptionalMembers>, additional_properties: Option<std::collections::HashMap<String, String>>) -> Address {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

crate:: is needed as Members and Address are defined in separate files when we use RustFileGenerator

@sonarcloud
Copy link

sonarcloud bot commented Sep 11, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@YushiOMOTE
Copy link
Contributor Author

Fix invalid type name included in generated Union types > (78c44a2)

I'm trying to give the example swagger or add a test in PR to describe this case soon 👀

This spec generates the issue with union.
YushiOMOTE@6f47a2e

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ReservedUnion {
#[serde(rename=\\"Student0\\")]
Student0(crate::Student),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This snapshot is for the issue fixed by this revision

Before the fix, the line became Student0(crate::Student0), which caused compile error.

@leigh-johnson
Copy link
Collaborator

leigh-johnson commented Sep 14, 2022

Sorry for the delay in responding! Thank you for sticking with this PR @YushiOMOTE 🙇‍♀️

The failing black-box tests are related to #847 , so don't worry about those.
https://github.com/asyncapi/modelina/actions/runs/3030534665/jobs/4877197283

Linter caught a couple of nits:
https://github.com/asyncapi/modelina/actions/runs/3030534665/jobs/4877197283

@jonaslagoni
Copy link
Member

Cant wait for this to be merged 😄 Just encountered the same issues 👍

Copy link
Collaborator

@leigh-johnson leigh-johnson left a comment

Choose a reason for hiding this comment

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

👋 I'd love to merge this @YushiOMOTE, just a few suggestions to pass the linter (added in review).

src/generators/rust/RustConstrainer.ts Outdated Show resolved Hide resolved
src/generators/rust/RustConstrainer.ts Outdated Show resolved Hide resolved
src/generators/rust/RustConstrainer.ts Outdated Show resolved Hide resolved
src/generators/rust/RustConstrainer.ts Outdated Show resolved Hide resolved
src/generators/rust/RustConstrainer.ts Outdated Show resolved Hide resolved
src/generators/rust/presets/CommonPreset.ts Outdated Show resolved Hide resolved
src/generators/rust/presets/CommonPreset.ts Outdated Show resolved Hide resolved
src/generators/rust/renderers/StructRenderer.ts Outdated Show resolved Hide resolved
src/generators/rust/renderers/UnionRenderer.ts Outdated Show resolved Hide resolved
src/helpers/FormatHelpers.ts Outdated Show resolved Hide resolved
YushiOMOTE and others added 9 commits November 5, 2022 12:49
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
@sonarcloud
Copy link

sonarcloud bot commented Nov 5, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Co-authored-by: Leigh Johnson <hi@leighjohnson.me>
@sonarcloud
Copy link

sonarcloud bot commented Nov 5, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@sonarcloud
Copy link

sonarcloud bot commented Nov 5, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@leigh-johnson
Copy link
Collaborator

@YushiOMOTE Looks like the last thing left is to update this snapshot 🎉 👏
https://github.com/asyncapi/modelina/actions/runs/3398564376/jobs/5652579555

@leigh-johnson leigh-johnson changed the base branch from next to cleanup-pr-869 November 16, 2022 18:02
@leigh-johnson
Copy link
Collaborator

@YushiOMOTE Just a heads up, I'm going to merge this PR into a new branch, cleanup-pr-869, and do the last bit of cleanup. Thank you for submitting these revisions!

@jonaslagoni
Copy link
Member

@all-contributors
please add @YushiOMOTE for bug, code.
please add @leigh-johnson for review.

@allcontributors
Copy link
Contributor

@jonaslagoni

I've put up a pull request to add @YushiOMOTE! 🎉

I've put up a pull request to add @leigh-johnson! 🎉

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

3 participants