2

I have a simple micronaut 3.0.3 application built with hibernate-gorm.

I get the following error when attempting to send a GET request like this http://localhost:8080/?commentId=1

Failed to convert argument [command] for value [null] due to: Specified value [1] is not of the correct type: class java.lang.Long (through reference chain: com.example.PostCommand[\"commentId\"])

This code worked in Micronaut version 2.5.12. I searched the migration guide about this but could not find any mentions about anything changing in this department.

PostCommand:

package com.example

import grails.gorm.annotation.Entity
import org.grails.datastore.gorm.GormEntity

@Entity
class PostCommand implements GormEntity<PostCommand> {

    String title
    String content
    Long commentId

    void setCommentId(Long commentId) {
        this.commentId = commentId
    }

    void setCommentId(String commentId) {
        this.commentId = Long.valueOf(commentId)
    }
}

PostContoller:

package com.example

import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get

@Controller('/')
class PostController {

    @Get('{?command*}')
    HttpResponse search(HttpRequest request, PostCommand command){
        HttpResponse.ok()
    }
}

When removing all setters or just the setter for the string type, it works.

Here is a github link for anyone interested.

Any suggestions would be helpful.

maun
  • 134
  • 1
  • 1
  • 13
  • I tested this with Spock in Micronaut 3.0.2, controller and command in Java, but without GormEntity, test passed. I believe you should consider using a clean view model for passing data to your controller. – Roar S. Oct 01 '21 at 19:03
  • @RoarS. Currently, rewriting all my domain objects is something I'm trying to prevent from happening. I feel that upgrading from 2.5.12 to 3.0.2 should have not broken the GormEntity functionality in this way. Could this be considered a bug in the framework itself? – maun Oct 03 '21 at 11:16
  • Why is it that you want both `void setCommentId(String commentId)` and `void setCommentId(Long commentId)` in your entity? – Jeff Scott Brown Nov 02 '21 at 17:09
  • @JeffScottBrown So that empty value`s cannot be passed in. – maun Nov 03 '21 at 07:12
  • "So that empty values cannot be passed in." - having overloaded setters like that does not prevent empty values. An empty String or `null` could be passed to `void setCommentId(String commentId)` and `null` could be passed in to `setCommentId(Long commentId)`. – Jeff Scott Brown Nov 03 '21 at 12:53

0 Answers0