Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for java.time.YearMonth as JAX-RS parameter #45699

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.UNI;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.URI_INFO;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.YEAR;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.YEAR_MONTH;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.ZONED_DATE_TIME;

import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -157,7 +158,7 @@ public abstract class EndpointIndexer<T extends EndpointIndexer<T, PARAM, METHOD
RESOURCE_INFO);

protected static final Set<DotName> SUPPORT_TEMPORAL_PARAMS = Set.of(INSTANT, LOCAL_DATE, LOCAL_TIME, LOCAL_DATE_TIME,
OFFSET_TIME, OFFSET_DATE_TIME, ZONED_DATE_TIME, YEAR);
OFFSET_TIME, OFFSET_DATE_TIME, ZONED_DATE_TIME, YEAR, YEAR_MONTH);

protected static final Logger log = Logger.getLogger(EndpointIndexer.class);
protected static final String[] EMPTY_STRING_ARRAY = new String[] {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -203,6 +204,7 @@ public final class ResteasyReactiveDotNames {
public static final DotName OFFSET_TIME = DotName.createSimple(OffsetTime.class.getName());
public static final DotName ZONED_DATE_TIME = DotName.createSimple(ZonedDateTime.class.getName());
public static final DotName YEAR = DotName.createSimple(Year.class.getName());
public static final DotName YEAR_MONTH = DotName.createSimple(YearMonth.class.getName());

public static final DotName UNI = DotName.createSimple(Uni.class.getName());
public static final DotName MULTI = DotName.createSimple(Multi.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.SET;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.SORTED_SET;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.YEAR;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.YEAR_MONTH;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.ZONED_DATE_TIME;

import java.io.File;
Expand Down Expand Up @@ -83,6 +84,7 @@
import org.jboss.resteasy.reactive.server.core.parameters.converters.RuntimeResolvedConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.SetConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.SortedSetConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.YearMonthParamConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.YearParamConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ZonedDateTimeParamConverter;
import org.jboss.resteasy.reactive.server.mapping.URITemplate;
Expand Down Expand Up @@ -623,6 +625,8 @@ private ParameterConverterSupplier determineTemporalConverter(DotName paramType,
return new ZonedDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (YEAR.equals(paramType)) {
return new YearParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (YEAR_MONTH.equals(paramType)) {
return new YearMonthParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
}

throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.jboss.resteasy.reactive.server.core.parameters.converters;

import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;

public class YearMonthParamConverter extends TemporalParamConverter<YearMonth> {

// lifted from the JDK as PARSER is private...
private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.toFormatter();

// this can be called by generated code
public YearMonthParamConverter() {
super(PARSER);
}

public YearMonthParamConverter(DateTimeFormatter formatter) {
super(formatter);
}

@Override
protected YearMonth convert(String value) {
return YearMonth.parse(value);
}

@Override
protected YearMonth convert(String value, DateTimeFormatter formatter) {
return YearMonth.parse(value, formatter);
}

public static class Supplier extends TemporalSupplier<YearMonthParamConverter> {

public Supplier() {
}

public Supplier(String pattern, String dateTimeFormatterProviderClassName) {
super(pattern, dateTimeFormatterProviderClassName);
}

@Override
protected YearMonthParamConverter createConverter(DateTimeFormatter dateTimeFormatter) {
return new YearMonthParamConverter(dateTimeFormatter);
}

@Override
public String getClassName() {
return YearMonthParamConverter.class.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.jboss.resteasy.reactive.server.vertx.test.simple;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

import jakarta.ws.rs.CookieParam;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;

import org.hamcrest.Matchers;
import org.jboss.resteasy.reactive.DateFormat;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.restassured.RestAssured;

public class YearMonthParamTest {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(HelloResource.class, CustomDateTimeFormatterProvider.class));

@Test
public void yearMonthAsQueryParam() {
RestAssured.get("/hello?date=01984-12")
.then().statusCode(200).body(Matchers.equalTo("hello#1984-12"));
}

@Test
public void yearMonthAsPathParam() {
RestAssured.get("/hello/1821-01")
.then().statusCode(200).body(Matchers.equalTo("hello@1821-01"));
}

@Test
public void yearMonthAsFormParam() {
RestAssured.given().formParam("date", "995-06").post("/hello")
.then().statusCode(200).body(Matchers.equalTo("hello:0995-06"));
}

@Test
public void yearMonthAsHeader() {
RestAssured.with().header("date", "1984-11")
.get("/hello/header")
.then().statusCode(200).body(Matchers.equalTo("hello=1984-11"));
}

@Test
public void yearMonthAsCookie() {
RestAssured.with().cookie("date", "1984-10")
.get("/hello/cookie")
.then().statusCode(200).body(Matchers.equalTo("hello/1984-10"));
}

@Path("hello")
public static class HelloResource {

@GET
public String helloQuery(@QueryParam("date") @DateFormat(pattern = "yyyyy-MM") YearMonth date) {
return "hello#" + date;
}

@GET
@Path("{date}")
public String helloPath(@PathParam("date") YearMonth date) {
return "hello@" + date;
}

@POST
public String helloForm(
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) YearMonth date) {
return "hello:" + date;
}

@GET
@Path("cookie")
public String helloCookie(
@CookieParam("date") YearMonth date) {
return "hello/" + date;
}

@GET
@Path("header")
public String helloHeader(
@HeaderParam("date") YearMonth date) {
return "hello=" + date;
}
}

public static class CustomDateTimeFormatterProvider implements DateFormat.DateTimeFormatterProvider {
@Override
public DateTimeFormatter get() {
return DateTimeFormatter.ofPattern("yyy-MM");
}
}

}
Loading