Skip to content

Commit e884ae6

Browse files
authored
Feature/1471 (#12)
* Added validation for uniqueness based on context and name for specifications. * Added release notes.
1 parent 12c27f7 commit e884ae6

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Java Client Library (beta)
2-
### v0.3.3
2+
### v0.3.4
33

44
## Introduction
55
Welcome my friends! This is the Poly API Java client GitHub page. If you are here, then it means you're familiar with what we do at Poly. If you aren't, you can always check [here](https://github.com/polyapi/poly-alpha).
@@ -347,6 +347,8 @@ Comparing to its Typescript counterpart, the Java library is still missing the f
347347
These features will be added in the future releases.
348348

349349
## Changelog
350+
### v0.3.4
351+
- Added validation to avoid duplicate specifications based on context and name.
350352
### v0.3.3
351353
- Fixed 'type' property for Server variables
352354
- Added missing 'type' property to specs.json

polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10+
import java.util.ArrayList;
11+
import java.util.HashMap;
1012
import java.util.List;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
1115

1216
import static com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance;
17+
import static java.lang.String.format;
18+
import static java.util.function.Function.identity;
1319
import static java.util.stream.Collectors.joining;
1420

1521
public class SpecificationServiceImpl extends PolyApiService implements SpecificationService {
@@ -25,8 +31,19 @@ public List<Specification> getJsonSpecs() {
2531
List<Specification> specifications = get("specs", defaultInstance().constructCollectionType(List.class, Specification.class));
2632
logger.info("{} specifications retrieved.", specifications.size());
2733
if (logger.isDebugEnabled()) {
28-
logger.trace("Retrieved specifications the following IDs: [{}]", specifications.stream().map(Specification::getId).collect(joining(", ")));
34+
logger.trace("Retrieved specifications with the following IDs: [{}]", specifications.stream().map(Specification::getId).collect(joining(", ")));
2935
}
30-
return specifications;
36+
logger.debug("Validating for duplicate context/name pairs.");
37+
Map<String, Specification> uniquenessValidationMap = new HashMap<>();
38+
specifications.forEach(specification -> {
39+
String key = format("%s.%s", specification.getContext(), specification.getName());
40+
if (uniquenessValidationMap.containsKey(key)) {
41+
logger.warn("Skipping {} specification '{}' in context '{}' as it clashes with {} specification with the same name and context.", specification.getType(), specification.getName(), specification.getContext(), uniquenessValidationMap.get(key).getType());
42+
} else {
43+
logger.debug("Specification key '{}' not repeated (yet).", key);
44+
uniquenessValidationMap.put(key, specification);
45+
}
46+
});
47+
return uniquenessValidationMap.values().stream().toList();
3148
}
3249
}

0 commit comments

Comments
 (0)