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

[FEATURE] Handle oneOf with null the same as other nullable types #2141

Open
2 tasks done
coekie opened this issue Dec 23, 2024 · 0 comments
Open
2 tasks done

[FEATURE] Handle oneOf with null the same as other nullable types #2141

coekie opened this issue Dec 23, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@coekie
Copy link

coekie commented Dec 23, 2024

Why do we need this improvement?

When a field is specified as being nullable using a oneOf, currently the java code (and I suppose some other languages too) that modelina generates contains an empty AnonymousSchema_ interface that is implemented by a single class.
The generated model would be more useable if that interface was dropped and usages replaced with the class.

Concretely, consider this example. The request here is that string2 and bar2 in this example would be treated the same as string1 and bar1, as they are both ways to express that a property is either null or has a value of a specific type.

asyncapi: 2.6.0
info:
  title: union problem
  version: "1"
defaultContentType: application/json
channels:
  /lala:
    publish:
      message:
        payload:
          type: object
          title: Foo
          additionalProperties: false
          properties:
            # This generates a property of type String
            string1:
              type: [ "string", "null" ]
            # This generates a property of type Object  
            string2:
              oneOf:
                - type: "null"
                - type: string
            # This generates a property of type Bar1    
            bar1:
              type: ["object", "null"]
              title: Bar1
              additionalProperties: false
            # This generates a property of type AnonymousSchema_*  
            bar2:
              oneOf:
                - type: "null"
                - type: object
                  title: Bar2
                  additionalProperties: false

In this example there seems to be an easy workaround: just write your schema like string1 and bar1, not using oneOf. But that is not always an option, e.g. when the type is using a reference with $ref and the referenced type is not nullable in all contexts where it is used. (Or if you are a consumer of such an API, not in control of the schema).

How will this change help?

This would make generated classesmore useful: simpler, readable, avoiding casts in usage.

Screenshots

No response

How could it be implemented/designed?

I would guess in the same way as properties with types like type: [ "string", "null" ]; but I am not familiar with modelina internals.

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

No, someone else can work on it.

@coekie coekie added the enhancement New feature or request label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant