Skip to content

Enhance support for java.time types in spring-web #22546

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

Closed
Closed
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 @@ -24,6 +24,7 @@
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -586,7 +587,14 @@ public List<String> getAccessControlExposeHeaders() {
* Set the (new) value of the {@code Access-Control-Max-Age} response header.
*/
public void setAccessControlMaxAge(long maxAge) {
set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge));
setAccessControlMaxAge(Duration.ofSeconds(maxAge));
}

/**
* Set the (new) value of the {@code Access-Control-Max-Age} response header.
*/
public void setAccessControlMaxAge(Duration maxAge) {
set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge.getSeconds()));
}

/**
Expand Down Expand Up @@ -932,6 +940,22 @@ public void setDate(long date) {
setDate(DATE, date);
}

/**
* Set the date and time at which the message was created, as specified
* by the {@code Date} header.
*/
public void setDate(Instant date) {
setInstant(DATE, date);
}

/**
* Set the date and time at which the message was created, as specified
* by the {@code Date} header.
*/
public void setDate(ZonedDateTime date) {
setZonedDateTime(DATE, date);
}

/**
* Return the date and time at which the message was created, as specified
* by the {@code Date} header.
Expand Down Expand Up @@ -985,6 +1009,14 @@ public void setExpires(long expires) {
setDate(EXPIRES, expires);
}

/**
* Set the date and time at which the message is no longer valid,
* as specified by the {@code Expires} header.
*/
public void setExpires(Instant expires) {
setInstant(EXPIRES, expires);
}

/**
* Return the date and time at which the message is no longer valid,
* as specified by the {@code Expires} header.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.cors;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -81,8 +82,7 @@ public class CorsConfiguration {
private Boolean allowCredentials;

@Nullable
private Long maxAge;

private Duration maxAge;

/**
* Construct a new {@code CorsConfiguration} instance with no cross-origin
Expand Down Expand Up @@ -310,6 +310,20 @@ public Boolean getAllowCredentials() {
* <p>By default this is not set.
*/
public void setMaxAge(@Nullable Long maxAge) {
if (maxAge == null) {
this.maxAge = null;
}
else {
this.maxAge = Duration.ofSeconds(maxAge);
}
}

/**
* Configure how long, in seconds, the response from a pre-flight request
* can be cached by clients.
* <p>By default this is not set.
*/
public void setMaxAge(@Nullable Duration maxAge) {
this.maxAge = maxAge;
}

Expand All @@ -319,7 +333,7 @@ public void setMaxAge(@Nullable Long maxAge) {
*/
@Nullable
public Long getMaxAge() {
return this.maxAge;
return this.maxAge != null ? this.maxAge.getSeconds() : null;
}

/**
Expand Down Expand Up @@ -353,7 +367,7 @@ public CorsConfiguration applyPermitDefaultValues() {
this.allowedHeaders = DEFAULT_PERMIT_ALL;
}
if (this.maxAge == null) {
this.maxAge = 1800L;
this.maxAge = Duration.ofSeconds(1800L);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.cors;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void setNullValues() {
assertNull(config.getExposedHeaders());
config.setAllowCredentials(null);
assertNull(config.getAllowCredentials());
config.setMaxAge(null);
config.setMaxAge((Duration)null);
assertNull(config.getMaxAge());
}

Expand Down