From ba58c59e2aec3c1d2a4d50787ed83aaaeb74b47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Ortiz?= Date: Wed, 21 Feb 2024 11:33:30 -0300 Subject: [PATCH 1/2] Added validation for uniqueness based on context and name for specifications. --- .../service/SpecificationServiceImpl.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java b/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java index 65fb028f..d6864e7c 100644 --- a/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java +++ b/polyapi-maven-plugin/src/main/java/io/polyapi/plugin/service/SpecificationServiceImpl.java @@ -7,9 +7,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import static com.fasterxml.jackson.databind.type.TypeFactory.defaultInstance; +import static java.lang.String.format; +import static java.util.function.Function.identity; import static java.util.stream.Collectors.joining; public class SpecificationServiceImpl extends PolyApiService implements SpecificationService { @@ -25,8 +31,19 @@ public List getJsonSpecs() { List specifications = get("specs", defaultInstance().constructCollectionType(List.class, Specification.class)); logger.info("{} specifications retrieved.", specifications.size()); if (logger.isDebugEnabled()) { - logger.trace("Retrieved specifications the following IDs: [{}]", specifications.stream().map(Specification::getId).collect(joining(", "))); + logger.trace("Retrieved specifications with the following IDs: [{}]", specifications.stream().map(Specification::getId).collect(joining(", "))); } - return specifications; + logger.debug("Validating for duplicate context/name pairs."); + Map uniquenessValidationMap = new HashMap<>(); + specifications.forEach(specification -> { + String key = format("%s.%s", specification.getContext(), specification.getName()); + if (uniquenessValidationMap.containsKey(key)) { + 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()); + } else { + logger.debug("Specification key '{}' not repeated (yet).", key); + uniquenessValidationMap.put(key, specification); + } + }); + return uniquenessValidationMap.values().stream().toList(); } } From efea868c5eaf174fd128f90c6b746157183049c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Ortiz?= Date: Wed, 21 Feb 2024 11:36:19 -0300 Subject: [PATCH 2/2] Added release notes. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cbdba6dd..8d330ab3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Java Client Library (beta) -### v0.3.3 +### v0.3.4 ## Introduction 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 These features will be added in the future releases. ## Changelog +### v0.3.4 +- Added validation to avoid duplicate specifications based on context and name. ### v0.3.3 - Fixed 'type' property for Server variables - Added missing 'type' property to specs.json