Skip to content

Commit 6aa8769

Browse files
committed
refactor: smrepo/smservice: use a different strategy for obtaining the return value
1 parent c55727a commit 6aa8769

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

basyx.submodelrepository/basyx.submodelrepository-http/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/http/SubmodelRepositoryApiHTTPController.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Arrays;
3232
import java.util.List;
3333

34+
import com.fasterxml.jackson.databind.ObjectMapper;
3435
import org.eclipse.digitaltwin.aas4j.v3.model.OperationRequest;
3536
import org.eclipse.digitaltwin.aas4j.v3.model.OperationResult;
3637
import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable;
@@ -45,6 +46,7 @@
4546
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier;
4647
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifierSize;
4748
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncoder;
49+
import org.eclipse.digitaltwin.basyx.http.CustomTypeCloneFactory;
4850
import org.eclipse.digitaltwin.basyx.http.pagination.Base64UrlEncodedCursor;
4951
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResult;
5052
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResultPagingMetadata;
@@ -77,12 +79,14 @@
7779
@RestController
7880
public class SubmodelRepositoryApiHTTPController implements SubmodelRepositoryHTTPApi {
7981

80-
private SubmodelRepository repository;
82+
private final SubmodelRepository repository;
83+
private final ObjectMapper objectMapper;
8184

8285
@Autowired
83-
public SubmodelRepositoryApiHTTPController(SubmodelRepository repository) {
86+
public SubmodelRepositoryApiHTTPController(SubmodelRepository repository, ObjectMapper objectMapper) {
8487
this.repository = repository;
85-
}
88+
this.objectMapper = objectMapper;
89+
}
8690

8791
@Override
8892
public ResponseEntity<Submodel> postSubmodel(@Parameter(in = ParameterIn.DEFAULT, description = "Submodel object", required = true, schema = @Schema()) @Valid @RequestBody Submodel body) {
@@ -177,14 +181,18 @@ public ResponseEntity<SubmodelElement> getSubmodelElementByPathSubmodelRepo(Base
177181

178182
@Override
179183
public ResponseEntity<SubmodelElement> postSubmodelElementByPathSubmodelRepo(Base64UrlEncodedIdentifier submodelIdentifier, String idShortPath, @Valid SubmodelElement body, @Valid String level, @Valid String extent) {
180-
repository.createSubmodelElement(submodelIdentifier.getIdentifier(), idShortPath, body);
181-
return new ResponseEntity<>(repository.getSubmodelElement(submodelIdentifier.getIdentifier(), idShortPath), HttpStatus.CREATED);
184+
SubmodelElement validatedBody = new CustomTypeCloneFactory<>(SubmodelElement.class, objectMapper).create(body);
185+
186+
repository.createSubmodelElement(submodelIdentifier.getIdentifier(), idShortPath, validatedBody);
187+
return new ResponseEntity<>(validatedBody, HttpStatus.CREATED);
182188
}
183189

184190
@Override
185191
public ResponseEntity<SubmodelElement> postSubmodelElementSubmodelRepo(Base64UrlEncodedIdentifier submodelIdentifier, @Valid SubmodelElement body) {
186-
repository.createSubmodelElement(submodelIdentifier.getIdentifier(), body);
187-
return new ResponseEntity<>(repository.getSubmodelElement(submodelIdentifier.getIdentifier(), body.getIdShort()), HttpStatus.CREATED);
192+
SubmodelElement validatedBody = new CustomTypeCloneFactory<>(SubmodelElement.class, objectMapper).create(body);
193+
194+
repository.createSubmodelElement(submodelIdentifier.getIdentifier(), validatedBody);
195+
return new ResponseEntity<>(validatedBody, HttpStatus.CREATED);
188196
}
189197

190198
@Override

basyx.submodelservice/basyx.submodelservice-http/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/http/SubmodelServiceHTTPApiController.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.InputStream;
3030
import java.util.List;
3131

32+
import com.fasterxml.jackson.databind.ObjectMapper;
3233
import org.eclipse.digitaltwin.aas4j.v3.model.OperationRequest;
3334
import org.eclipse.digitaltwin.aas4j.v3.model.OperationResult;
3435
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
@@ -38,6 +39,7 @@
3839
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
3940
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
4041
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
42+
import org.eclipse.digitaltwin.basyx.http.CustomTypeCloneFactory;
4143
import org.eclipse.digitaltwin.basyx.http.pagination.Base64UrlEncodedCursor;
4244
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResult;
4345
import org.eclipse.digitaltwin.basyx.http.pagination.PagedResultPagingMetadata;
@@ -69,12 +71,14 @@
6971
@RestController
7072
public class SubmodelServiceHTTPApiController implements SubmodelServiceHTTPApi {
7173

72-
private SubmodelService service;
74+
private final SubmodelService service;
75+
private final ObjectMapper objectMapper;
7376

7477
@Autowired
75-
public SubmodelServiceHTTPApiController(SubmodelService service) {
78+
public SubmodelServiceHTTPApiController(SubmodelService service, ObjectMapper objectMapper) {
7679
this.service = service;
77-
}
80+
this.objectMapper = objectMapper;
81+
}
7882

7983
@Override
8084
public ResponseEntity<Void> deleteSubmodelElementByPath(
@@ -196,16 +200,21 @@ public ResponseEntity<Void> patchSubmodelElementByPathValueOnly(
196200
public ResponseEntity<SubmodelElement> postSubmodelElement(@Parameter(in = ParameterIn.DEFAULT, description = "Requested submodel element", required = true, schema = @Schema()) @Valid @RequestBody SubmodelElement body,
197201
@Parameter(in = ParameterIn.QUERY, description = "Determines the structural depth of the respective resource content", schema = @Schema(allowableValues = { "deep",
198202
"core" }, defaultValue = "deep")) @Valid @RequestParam(value = "level", required = false, defaultValue = "deep") String level) {
199-
service.createSubmodelElement(body);
200-
return new ResponseEntity<>(service.getSubmodelElement(body.getIdShort()), HttpStatus.CREATED);
203+
204+
SubmodelElement validatedBody = new CustomTypeCloneFactory<>(SubmodelElement.class, objectMapper).create(body);
205+
service.createSubmodelElement(validatedBody);
206+
207+
return new ResponseEntity<>(validatedBody, HttpStatus.CREATED);
201208
}
202209

203210
@Override
204211
public ResponseEntity<SubmodelElement> postSubmodelElementByPath(
205212
@Parameter(in = ParameterIn.PATH, description = "IdShort path to the submodel element (dot-separated)", required = true, schema = @Schema()) @PathVariable("idShortPath") String idShortPath,
206213
@Parameter(in = ParameterIn.DEFAULT, description = "Requested submodel element", required = true, schema = @Schema()) @Valid @RequestBody SubmodelElement body) {
207-
service.createSubmodelElement(idShortPath, body);
208-
return new ResponseEntity<>(service.getSubmodelElement(idShortPath), HttpStatus.CREATED);
214+
SubmodelElement validatedBody = new CustomTypeCloneFactory<>(SubmodelElement.class, objectMapper).create(body);
215+
service.createSubmodelElement(idShortPath, validatedBody);
216+
217+
return new ResponseEntity<>(validatedBody, HttpStatus.CREATED);
209218
}
210219

211220
@Override

0 commit comments

Comments
 (0)