Skip to content

Form-url-encoded body with JsonProperty annotation and http client #1853

Open
@sdelamo

Description

Steps to Reproduce

  1. create a sample mn create-app example.micronaut --inplace

  2. Create a controller which consumes form-url-encoded with a POJO with a field annotated with @JsonProperty.

package example.micronaut;

import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Status;

import javax.validation.Valid;

@Controller
public class HomeController {

    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Status(HttpStatus.OK)
    @Post
    public void index(@Valid @Body DeviceAuthentication device) {
    }
}
package example.micronaut;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.core.annotation.Introspected;

import javax.annotation.Nonnull;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

@Introspected
public class DeviceAuthentication {

    @JsonProperty("client_id")
    @Nonnull
    @NotBlank
    private String clientId;

    public DeviceAuthentication() {

    }

    public DeviceAuthentication(String clientId) {
        this.clientId = clientId;
    }

    @Nonnull
    public String getClientId() {
        return clientId;
    }

    public void setClientId(@Nonnull String clientId) {
        this.clientId = clientId;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> m = new HashMap<>();
        m.put("client_id", getClientId());
        return m;
    }
}
package example.micronaut

import io.micronaut.context.ApplicationContext
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus
import io.micronaut.http.MediaType
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

class HomeControllerSpec extends Specification {

    @AutoCleanup
    @Shared
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)

    ApplicationContext getApplicationContext() {
        embeddedServer.applicationContext
    }

    @Shared
    HttpClient httpClient = applicationContext.createBean(HttpClient, embeddedServer.URL)

    BlockingHttpClient getClient() {
        httpClient.toBlocking()
    }

    void "endpoint can be called with non declarative client if the field name is used"() {
        given:
        HttpRequest request = HttpRequest.POST('/', new DeviceAuthentication('12345').toMap())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)

        when:
        HttpResponse rsp = client.exchange(request)

        then:
        rsp.status() == HttpStatus.OK
    }


    void "endpoint can be called with non declarative client"() {
        given:
        HttpRequest request = HttpRequest.POST('/', new DeviceAuthentication('12345'))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)

        when:
        HttpResponse rsp = client.exchange(request)

        then:
        rsp.status() == HttpStatus.OK
    }
}

Expected Behaviour

Both test pass.

Actual Behaviour

Last test fails.

Environment Information

  • Operating System: MacOS Mojave
  • Micronaut Version: 1.2.0.RC1
  • JDK Version: 1.8.0_191

Example Application

json-property-url-encoded.zip

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions