Skip to content

ENH: Support Kotlin non-nullable properties and constructor params #22

Closed
@rbygrave

Description

@rbygrave

So for example when marshalling into @Post @Form the following dto:

  // our form bean ... with Kotlin non-nullable types

  data class HelloForm(var email: String, var url: String, var description: String)

   // used by @Form @Post controller method

  @Form @Post("asform")
  fun saveForm(hi: HelloForm) {
    println("saving $hi")
  }

The generated code understands that the properties are Kotlin non-nullable types so checks for null when populating HelloForm.

The generated code is:

    ApiBuilder.post("/hello/asform", ctx -> {
      ctx.status(201);
      HelloForm hi =  new HelloForm(
        checkNull(ctx.formParam("email"), "email"), 
        checkNull(ctx.formParam("url"), "url"), 
        checkNull(ctx.formParam("description"), "description")
      );

      controller.saveForm(hi);
    });

Errors

Then if we post to that form and don't have a property we get an exception like:

io.dinject.controller.RequiredArgumentException: Required property description was not supplied.
	at io.dinject.controller.PathTypeConversion.checkNull(PathTypeConversion.java:26)
	at org.example.web.HelloController$route.lambda$registerRoutes$2(HelloController$route.java:43)
...

So we typically register an exception handler for io.dinject.controller.RequiredArgumentException

    app.exception(RequiredArgumentException::class.java) { exception, ctx ->
      val map = LinkedHashMap<String, String?>()
      map["message"] = exception.message
      ctx.json(map)
      ctx.status(400)
    }

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions