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 an annotation allowing to explicitly declare if an input is editable or not #2129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/main/java/org/springframework/hateoas/InputEdition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Annotation to explicitly declare if an input is editable or not.
*
* @author Réda Housni Alaoui
* @since 2.3
*/
@Documented
@Retention(RUNTIME)
@Target({ FIELD, METHOD, ANNOTATION_TYPE })
public @interface InputEdition {

Mode value();

enum Mode {
READ_ONLY, READ_WRITE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.springframework.hateoas.AffordanceModel.PropertyMetadata;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.InputEdition;
import org.springframework.hateoas.InputType;
import org.springframework.http.HttpEntity;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -471,6 +472,11 @@ public boolean isRequired() {
@Override
public boolean isReadOnly() {

MergedAnnotation<InputEdition> inputEditionAnnotation = property.getAnnotation(InputEdition.class);
if (inputEditionAnnotation.isPresent()) {
return inputEditionAnnotation.getEnum("value", InputEdition.Mode.class) == InputEdition.Mode.READ_ONLY;
}

if (!property.hasWriteMethod()) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.hateoas.AffordanceModel.PayloadMetadata;
import org.springframework.hateoas.AffordanceModel.PropertyMetadata;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.InputEdition;
import org.springframework.hateoas.InputType;
import org.springframework.hateoas.mediatype.html.HtmlInputType;
import org.springframework.hateoas.server.core.MethodParameters;
Expand Down Expand Up @@ -148,6 +149,14 @@ void considersAccessorAvailablility() {
assertThat(getProperty(metadata, "readOnly")) //
.map(PropertyMetadata::isReadOnly) //
.hasValue(true);

assertThat(getProperty(metadata, "getterWithInputEditionEnabled"))
.map(PropertyMetadata::isReadOnly)
.hasValue(false);

assertThat(getProperty(metadata, "setterWithInputEditionDisabled"))
.map(PropertyMetadata::isReadOnly)
.hasValue(true);
}

@Test
Expand Down Expand Up @@ -294,6 +303,8 @@ static class MethodExposurePayload {

@Getter @Setter String readWrite;
@Getter String readOnly;
@Getter @InputEdition(InputEdition.Mode.READ_WRITE) String getterWithInputEditionEnabled;
@Getter @Setter @InputEdition(InputEdition.Mode.READ_ONLY) String setterWithInputEditionDisabled;
}

@RestController
Expand Down
Loading