icyleaf / swagger

Swagger contains a OpenAPI / Swagger universal documentation generator and HTTP server handler.
MIT License
41 stars 10 forks source link

Improve Swagger Object creation #26

Open MathiusD opened 2 years ago

MathiusD commented 2 years ago

Add creation of Swagger::Object by some object instance

(Related and insipred by #18)

Allow creation of Swagger::Object by something like that :

struct User
  property id, nickname, username, email, bio

  def initialize(@id : String, @nickname : String, @username : String, @email : String, @bio : String? = nil)
  end
end

user = User.new(
  UUID.random.to_s, "icyleaf wang", "icyleaf", "icyleaf.cn@gmail.com", "Personal bio"
)

builder.add(Swagger::Object.create_from_instance(user))

P.S. : src/swagger/http/handler.cr was modified when the code formatting was executed via crystal tool format src && crystal tool format spec. I have therefore committed the formatting.

icyleaf commented 2 years ago

Thanks for the PR, I reviewed the code that the current implementation only contains the base type, so if there are complex types or nested variables such an implementation would be exceptionally complicated, my suggestion is to see if Crystal implements reflection natively.

MathiusD commented 2 years ago

Thanks for the PR, I reviewed the code that the current implementation only contains the base type, so if there are complex types or nested variables such an implementation would be exceptionally complicated, my suggestion is to see if Crystal implements reflection natively.

Indeed, I should have put this PR in Draft. I still have to do the following:

However I don't understand how the implementation would be harder without native reflection, indeed, for schemas the open-api spec the other schema and enums are pointed by reference. (In addition reflection made here via macros allows to get all the necessary information, in my opinion)

The only point which seems to me to pose problem intuitively is the management of the Union other than SomeClass? since we could not use it in the same way.