oatpp / oatpp-swagger

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

Initial implementation of defaults for swagger #24

Closed natanael closed 4 years ago

natanael commented 4 years ago

Following the issue oatpp/oatpp-swagger#23 I don't think this serves as a complete solution for the defaults in swagger, as it should match the type of the field and I implemented it as a string in all cases. The word default is quite tricky as well so I went with default_value, if anything it makes it more explicit.

This matches: https://github.com/oatpp/oatpp/pull/276

I'm open to all suggestions.

natanael commented 4 years ago

I don't know how to get the representation of a Void defaultValue. It certainly does not help that I could not setup a good IntelliSense. I will give it another try as soon as possible, but if that is super simple or I just missed something please let me know.

Thanks for helping me contribute!

natanael commented 4 years ago

I edited the UserDto so it would have a String field with default:

class UserDto : public oatpp::DTO {

  DTO_INIT(UserDto, DTO)

  DTO_FIELD(Int32, id);
  DTO_FIELD(String, firstName, "first-name");
  DTO_FIELD(String, lastName, "last-name");
  DTO_FIELD(String, referral, "referral") = "direct"; // <- this
  DTO_FIELD(List<String>, friends) = List<String>::createShared();

  DTO_FIELD(Enum<HelloEnum>, helloEnum);

};

And the result is:

"UserDto": {
  "type": "object",
  "properties": {
    "id": {
      "type": "integer",
      "default": null,
      "minimum": -2147483648,
      "maximum": 2147483647
    },
    "first-name": {
      "type": "string",
      "default": null
    },
    "last-name": {
      "type": "string",
      "default": null
    },
    "referral": {
      "type": "string",
      "default": "integer" // <- This should be "direct", we are getting the type not the value somehow
    },
    "friends": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "helloEnum": {
      "$ref": "#\/components\/schemas\/HelloEnum(String)"
    }
  }
}
lganzzzo commented 4 years ago

Great PR! 🎉 Merged!