oatpp / oatpp-swagger

OpenApi 3.0.0 docs + Swagger UI for oatpp services
https://oatpp.io/
Apache License 2.0
91 stars 51 forks source link

Annotation of DTO #20

Closed yeefan closed 3 years ago

yeefan commented 4 years ago

Hi, is there a way to annotate

such that the annotated descriptions for the DTO show up in the OpenAPI document and hence Swagger UI?

Additionally, support for enum fields in a DTO (in a manner similar to #19) would be much appreciated.

Thanks!

lganzzzo commented 4 years ago

Hello @yeefan ,

Thanks for the question!

Currently, there is no way to annotate the individual fields of DTO. Also, as far as I know, it's a limitation of swagger specification currently - According to Schema Object specification you can't put a description to object fields.

As for annotating the DTO, you may consider the feature introduced in https://github.com/oatpp/oatpp/issues/212 Basically you can add a description to a response:

  ENDPOINT_INFO(root) {
    info->summary = "root_summary";
    info->addResponse<MyDto::ObjectWrapper>(Status::CODE_200, "text/plain", "Your custom description");
  }
  ENDPOINT("GET", "/", root) {
    auto myDto = MyDto::createShared();
    ...
    return createResponse(Status::CODE_200, myDto);
  }

Additionally, support for enum fields in a DTO (in a manner similar to #19) would be much appreciated.

Most probably this feature will be introduced in 1.1.0 - the next release.

Best Regards, Leonid

yeefan commented 4 years ago

Hi Leonid,

I admit I am very new to Swagger or OpenAPI, but in the Java world, I was able to get field descriptions to show up when I use the Spring Framework together with SpringFox annotations, so I am rather intrigued as to why I can't do likewise in the C++ world.

In the link you gave me, it appears that it is possible to add "description" fields when describing a model, both at the object level as well as the field level. See for example the following Cat schema, which incidentally also contains an enum field with a description:

{
  "components": {
    "schemas": {
      ...
      "Cat": {
        "description": "A representation of a cat. Note that `Cat` will be used as the discriminator value.",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "huntingSkill": {
                "type": "string",
                "description": "The measured skill for hunting",
                "default": "lazy",
                "enum": [
                  "clueless",
                  "lazy",
                  "adventurous",
                  "aggressive"
                ]
              }
            },
            "required": [
              "huntingSkill"
            ]
          }
        ]
      },
      ...
    }
  }
}
lganzzzo commented 4 years ago

Hey @yeefan ,

You are absolutely right! I was looking into the schema specification before sending the response to you, and I just didn't see it there :). Thanks for double-checking!

Then, we'll have to investigate the way of adding this functionality to oatpp...

Maybe it can be something like this (same as in ApiController):

class Cat : public oatpp::Object {

  DTO_INIT(Cat, Object)

  DTO_FIELD_INFO(huntingSkill) { // <--- hypothetical code
    info->description = "The measured skill for hunting";
  }
  DTO_FIELD(String, huntingSkill);

}

@bhorn , what do you think?

@yeefan , thanks again for double-checking! Best Regards, Leonid

yeefan commented 4 years ago

Thanks!

To be clear, what I am looking for is the ability to have Swagger UI display descriptions and enum values as illustrated in the following example, taken from Swagger Petstore (http://petstore.swagger.io:8080/):

image

So perhaps the ability to do something like the following would be nice. :)

  DTO_FIELD_INFO(huntingSkill) { // <--- hypothetical code
    info->description = "The measured skill for hunting";
    info->enumValues = {"clueless", "lazy", "adventurous", "aggressive"};
  }

Having said that, for completeness sake (although I am not specifically requesting for them), you might also consider allowing users to specify other properties in object fields that you can specify in path parameters and query parameters, such as default value, minimum and maximum values, regular expressions, and so on. As far as I know, these can be done with SpringFox.

Thanks again.

lganzzzo commented 4 years ago

If we are able to make it on time, we'll ship it with the nearest 1.1.0. If not, it will be scheduled for the next release.

Regards, Leonid

bamkrs commented 4 years ago

This definitely sounds like a great change for 1.1.0 and would fit nicely to the other enum-related requests. I have several ideas how one could accomplish that. Either with a DTO-Registry which registers all dtos in DTO_INIT and then use the same process as in the API-Controller or - the easy way - let the users register all DTO's by themselfs after adding the DTO_FIELD_INFO.

lganzzzo commented 4 years ago

Hey @bhorn ,

We have a static Property object associated with each field. It should be pretty easy to extend the Property with the needed information. And then get all the info from there.

The class property - https://github.com/oatpp/oatpp/blob/master/src/oatpp/core/data/mapping/type/Type.hpp#L331

lganzzzo commented 4 years ago

Hey @yeefan ,

The support for Enum and for DTO_FIELD_INFO is already added in the latest oatpp and oatpp-swagger.

Please see the oatpp changelog for details.

Regards, Leonid