kbuntrock / openapi-maven-plugin

Generate openapi documentation for SpringMVC or JaxRS/JakartaRS projects.
https://kbuntrock.github.io/openapi-maven-plugin/
MIT License
13 stars 8 forks source link

Optional in interface not supported #106

Closed magx2 closed 2 months ago

magx2 commented 5 months ago

I just found that if you have Optional in interface it does not get unpacked:

public interface Foo {
    Optional<String> getOptionalFoo();
}

Produces this YML:

components:
  schemas:
    Foo:
      type: object
      properties:
        optionalFoo: # here
          type: object
          properties:
            value:
              type: string

instead of unpacked Optional:

components:
  schemas:
    Foo:
      type: object
      properties:
        optionalFoo: # here
          type: string

If you want to reproduce it you can look at my repo: https://github.com/magx2/openapi-maven-plugin-error

kbuntrock commented 4 months ago

I don't really know how to handle this case since we often consider "Optional" as a java syntaxic sugar - not translated into json.

For example, with Spring MVC with a default configuration, if you return an optional in a controller : it return the original object or null.

Do you have the use case only for you Dtos? Or for your api body/return contracts declarations also? *

Example for the second case : Optional getSomeData(@RequestParam Optional id)

magx2 commented 4 months ago

I think that there is a misunderstanding.

If I have an optional in class it's being unpacked to concrete type. On the other side optional in interfaces are not unpacked.

I would like to see the same behavior in interfaces as we have with classes

kbuntrock commented 4 months ago

Oh, got it! Indeed, I got it wrong. This is a fine request, added to the todo. :)

magx2 commented 3 months ago

Same problem would appear in parameters:

public String optionalParameter(@RequestParam("optional") Optional<String> optional) {
    return optional.orElse("default");
}
parameters:
  - name: optional
    in: query
    required: true # HERE!
    schema:
      type: object
      properties:
        value:
          type: string
kbuntrock commented 3 months ago

I think that there is a misunderstanding.

If I have an optional in class it's being unpacked to concrete type. On the other side optional in interfaces are not unpacked.

I would like to see the same behavior in interfaces as we have with classes

Are you sure it is linked to class vs interface? I could not reproduce this bug. For me the only unwrapping actually in the app is if an Optional is used as a webservice response. Everywhere else, there is no unwrapping. (before the PR I'm working on)

magx2 commented 3 months ago

Here you have code to reproduce it https://github.com/magx2/openapi-maven-plugin-error/commit/198880cdd3cb9ee233e4bb074e88ed82d8daf082

It's not a problem of not unpacking Optional in interface but rather not setting required to false. I thought it's similar code space so I've pointed it here. If you want you can create new issue

kbuntrock commented 2 months ago

Ok, got it. No, it's ok, no worries, I'll keep working on my current MR.

kbuntrock commented 2 months ago

Hello Martin. Do you mind testing if you have the time the current version on the "dev" branch? And check if it fix this bug well for you?

Thank you very much by advance. ;)

magx2 commented 2 months ago

Works for me :). LGTM!

sob., 24 lut 2024 o 16:13 Kévin Buntrock @.***> napisał(a):

Hello Martin. Do you mind testing if you have the time the current version on the "dev" branch? And check if it fix this bug well for you?

Thank you very much by advance. ;)

— Reply to this email directly, view it on GitHub https://github.com/kbuntrock/openapi-maven-plugin/issues/106#issuecomment-1962397890, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAWTLHQN5LH65DC2NUJR66LYVH7Q3AVCNFSM6AAAAABAWCR4MSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRSGM4TOOBZGA . You are receiving this because you authored the thread.Message ID: @.***>

-- Pozdrawiam Martin Grześlowski

kbuntrock commented 2 months ago

Nice, thank you :)