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

[BUG] generates broken Python code, default kwarg repeated #2174

Open
2 tasks done
black-snow opened this issue Jan 29, 2025 · 3 comments
Open
2 tasks done

[BUG] generates broken Python code, default kwarg repeated #2174

black-snow opened this issue Jan 29, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@black-snow
Copy link

Describe the bug.

I just bumped "@asyncapi/cli": "2.14.1" to "@asyncapi/cli": "2.16.0" and was surprised to see my tests fail, cuz I didn't see any relevant changes in the release notes.

What I found was that there are now serializers and validators, which seems fine, but asyncapi generate models python doc/asyncapi.yaml --pyDantic generates broken code now:

class AnonymousSchema1(BaseModel): 
  id: str = Field()
  source: str = Field()
  specversion: str = Field()
  type: str = Field()
  datacontenttype: Optional[str] = Field(default=None, default=None)
  dataschema: Optional[str] = Field(default=None, default=None)
  subject: Optional[str] = Field(default=None, default=None)
  time: Optional[str] = Field(default=None, default=None)
  data: Optional[Union[dict[str, Any], List[Any], str, float, bool]] = Field(default=None, default=None)
  data_base64: Optional[str] = Field(default=None, default=None)
  additional_properties: Optional[dict[str, Any]] = Field(default=None, exclude=True, default=None)

The kwarg default is repeated everywhere.

(It also has some unused imports, which bugs me a little :D)

Expected behavior

Generates valid Python code.

Screenshots

How to Reproduce

  1. grab an asyncapi example spec
  2. add a nullable type (I guess), or just $ref https://raw.githubusercontent.com/cloudevents/spec/v1.0.2/cloudevents/formats/cloudevents.json
  3. asyncapi generate models python doc/asyncapi.yaml --pyDantic

🥦 Browser

None

👀 Have you checked for similar open issues?

  • I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

@black-snow black-snow added the bug Something isn't working label Jan 29, 2025
Copy link
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup 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.

@derberg derberg transferred this issue from asyncapi/generator Jan 29, 2025
@derberg
Copy link
Member

derberg commented Jan 29, 2025

Moved to modelina as model generation comes from here, but to be honest, I don't know why errors are in CLI now as there was no upgrade of modelina CLI in asyncapi CLI for a while

@black-snow
Copy link
Author

black-snow commented Jan 30, 2025

Glancing over the code I can spot https://github.com/asyncapi/modelina/blob/master/src/generators/python/presets/Pydantic.ts#L53 and https://github.com/asyncapi/modelina/blob/master/src/generators/python/presets/Pydantic.ts#L62 - if I interpret this correctly, it means that this can happen when !property.required && property.property.options.isNullable.

Looks like this bug has been introduced with e9e46b3 two days ago o_Ò
My package-lock lists modelina with 4.0.0+

So the second time default=None is set would only cover the case where !required && !isNullable, which seems just wrong to me. If it's not nullable the default cannot be None (read: null) regardless of whether it's required or not.

Hence, I guess the fix would be to just drop L62-64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants