From fb6f2c0fed13fadf73829b0956003470ef9840c8 Mon Sep 17 00:00:00 2001 From: Mark Czotter Date: Sat, 17 Jun 2023 11:50:37 +0200 Subject: [PATCH] fix(api): ensure we use the same limit,field,sort,expand query param... ...names everywhere This commit fixes an incorrect `fields` parameter being used when fetching versions or commits in resource API. --- .../bundle/BundleResourceTypeConverter.java | 6 +++--- .../core/request/ResourceConverter.java | 8 ++++---- .../request/expand/BaseResourceExpander.java | 8 ++++++++ .../core/request/expand/ResourceExpander.java | 15 +++++---------- .../converter/SnomedConceptConverter.java | 18 +++++++++--------- .../converter/SnomedReferenceSetConverter.java | 10 ++++------ 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/bundle/BundleResourceTypeConverter.java b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/bundle/BundleResourceTypeConverter.java index ea344ae6668..59eb6233bce 100644 --- a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/bundle/BundleResourceTypeConverter.java +++ b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/bundle/BundleResourceTypeConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 B2i Healthcare Pte Ltd, http://b2i.sg + * Copyright 2021-2023 B2i Healthcare Pte Ltd, http://b2i.sg * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,7 +27,7 @@ import com.b2international.snowowl.core.internal.ResourceDocument; import com.b2international.snowowl.core.plugin.Component; import com.b2international.snowowl.core.request.ResourceRequests; -import com.b2international.snowowl.core.request.expand.ResourceExpander; +import com.b2international.snowowl.core.request.expand.BaseResourceExpander; /** * @since 8.0 @@ -58,7 +58,7 @@ public void expand(RepositoryContext context, Options expan results.forEach(bundle -> { final Resources resources = ResourceRequests.prepareSearch() .filterByBundleAncestorId(bundle.getId()) - .setLimit(expandOptions.containsKey("limit") ? expandOptions.get("limit", Integer.class) : ResourceExpander.DEFAULT_LIMIT) + .setLimit(BaseResourceExpander.getLimit(expandOptions)) .build() .execute(context); bundle.setProperties("content", resources); diff --git a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/ResourceConverter.java b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/ResourceConverter.java index 1feaf5b39b4..d4705b3efaa 100644 --- a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/ResourceConverter.java +++ b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/ResourceConverter.java @@ -144,8 +144,8 @@ public static void expandVersions(List results, Options expa Versions versions = ResourceRequests.prepareSearchVersion() .filterByResource(res.getResourceURI()) .setLimit(getLimit.apply(expandOptions)) - .setFields(expandOptions.containsKey("fields") ? expandOptions.getList("fields", String.class) : null) - .sortBy(expandOptions.containsKey("sort") ? expandOptions.getString("sort") : null) + .setFields(expandOptions.containsKey(FIELD_OPTION_KEY) ? expandOptions.getList(FIELD_OPTION_KEY, String.class) : null) + .sortBy(expandOptions.containsKey(SORT_OPTION_KEY) ? expandOptions.getString(SORT_OPTION_KEY) : null) .setLocales(locales) .build() .execute(context); @@ -165,8 +165,8 @@ private void expandCommits(List results) { return RepositoryRequests.commitInfos().prepareSearchCommitInfo() .filterByBranch(res.getBranchPath()) .setLimit(getLimit(expandOptions)) - .setFields(expandOptions.containsKey("fields") ? expandOptions.getList("fields", String.class) : CommitInfo.Fields.DEFAULT_FIELD_SELECTION) - .sortBy(expandOptions.containsKey("sort") ? expandOptions.getString("sort") : null) + .setFields(expandOptions.containsKey(FIELD_OPTION_KEY) ? expandOptions.getList(FIELD_OPTION_KEY, String.class) : CommitInfo.Fields.DEFAULT_FIELD_SELECTION) + .sortBy(expandOptions.containsKey(SORT_OPTION_KEY) ? expandOptions.getString(SORT_OPTION_KEY) : null) .setLocales(locales()) .build(res.getToolingId()) .executeWithContext(context()) diff --git a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/BaseResourceExpander.java b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/BaseResourceExpander.java index 6855a69394e..3c24cde3823 100644 --- a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/BaseResourceExpander.java +++ b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/BaseResourceExpander.java @@ -51,5 +51,13 @@ protected ServiceProvider context() { protected final List locales() { return locales; } + + public static final int getLimit(final Options expandOptions) { + return getLimit(expandOptions, DEFAULT_LIMIT); + } + + public static final int getLimit(final Options expandOptions, final int defaultLimit) { + return expandOptions.containsKey(LIMIT_OPTION_KEY) ? expandOptions.get(LIMIT_OPTION_KEY, Integer.class) : defaultLimit; + } } diff --git a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/ResourceExpander.java b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/ResourceExpander.java index 69da0081524..260403ce4f4 100644 --- a/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/ResourceExpander.java +++ b/core/com.b2international.snowowl.core/src/com/b2international/snowowl/core/request/expand/ResourceExpander.java @@ -19,8 +19,6 @@ import java.util.List; -import com.b2international.commons.options.Options; - import net.jodah.typetools.TypeResolver; /** @@ -32,6 +30,11 @@ public interface ResourceExpander { // The "default default" limit to use when no limit is given int DEFAULT_LIMIT = 50; + String LIMIT_OPTION_KEY = "limit"; + String FIELD_OPTION_KEY = "field"; + String SORT_OPTION_KEY = "sort"; + String EXPAND_OPTION_KEY = "expand"; + /** * Expands resources with additional fields. * @@ -39,14 +42,6 @@ public interface ResourceExpander { */ void expand(List results); - default int getLimit(final Options expandOptions) { - return getLimit(expandOptions, DEFAULT_LIMIT); - } - - default int getLimit(final Options expandOptions, final int defaultLimit) { - return expandOptions.containsKey("limit") ? expandOptions.get("limit", Integer.class) : defaultLimit; - } - /** * @return the class of the target type this expander can expand */ diff --git a/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedConceptConverter.java b/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedConceptConverter.java index c69097eaf33..c94138f50dd 100644 --- a/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedConceptConverter.java +++ b/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedConceptConverter.java @@ -255,9 +255,9 @@ private void expandDescriptions(List results, final Set c .filterByActive(expandOptions.get("active", Boolean.class)) .filterByType(expandOptions.get("typeId", String.class)) .filterByConcepts(conceptIds) - .setFields(expandOptions.containsKey("field") ? expandOptions.getList("field", String.class) : null) + .setFields(expandOptions.containsKey(FIELD_OPTION_KEY) ? expandOptions.getList(FIELD_OPTION_KEY, String.class) : null) .setLocales(locales()) - .sortBy(expandOptions.get("sort", String.class)) + .sortBy(expandOptions.get(SORT_OPTION_KEY, String.class)) .build() .execute(context()); @@ -284,10 +284,10 @@ private void expandRelationships(List results, final Set .filterByTypes(expandOptions.containsKey("typeId") ? expandOptions.getCollection("typeId", String.class) : null) .filterByDestinations(expandOptions.containsKey("destinationId") ? expandOptions.getCollection("destinationId", String.class) : null) .filterBySources(conceptIds) - .setFields(expandOptions.containsKey("field") ? expandOptions.getList("field", String.class) : null) - .setExpand(expandOptions.get("expand", Options.class)) + .setFields(expandOptions.containsKey(FIELD_OPTION_KEY) ? expandOptions.getList(FIELD_OPTION_KEY, String.class) : null) + .setExpand(expandOptions.get(EXPAND_OPTION_KEY, Options.class)) .setLocales(locales()) - .sortBy(expandOptions.get("sort", String.class)) + .sortBy(expandOptions.get(SORT_OPTION_KEY, String.class)) .build() .execute(context()); @@ -316,10 +316,10 @@ private void expandInboundRelationships(List results, final Set results, Set conceptIds .all() .filterByIds(componentIds) .setLocales(locales()) - .setExpand(expandOptions.get("expand", Options.class)) + .setExpand(expandOptions.get(EXPAND_OPTION_KEY, Options.class)) .build().execute(context()); final Map ancestorsById = newHashMap(); diff --git a/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedReferenceSetConverter.java b/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedReferenceSetConverter.java index f243a9a6a89..ad252e5d8ee 100644 --- a/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedReferenceSetConverter.java +++ b/snomed/com.b2international.snowowl.snomed.datastore/src/com/b2international/snowowl/snomed/datastore/converter/SnomedReferenceSetConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2022 B2i Healthcare Pte Ltd, http://b2i.sg + * Copyright 2011-2023 B2i Healthcare Pte Ltd, http://b2i.sg * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import com.b2international.snowowl.core.date.EffectiveTimes; import com.b2international.snowowl.core.domain.BranchContext; import com.b2international.snowowl.core.request.BaseRevisionResourceConverter; +import com.b2international.snowowl.core.request.expand.BaseResourceExpander; import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet; import com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets; import com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument; @@ -56,12 +57,9 @@ private void expandMembers(List results) { SnomedRefSetMemberSearchRequestBuilder req = SnomedRequests.prepareSearchMember() .filterByRefSet(refSet.getId()) .setLocales(locales()) - .setExpand(expandOptions.get("expand", Options.class)); + .setExpand(expandOptions.get(EXPAND_OPTION_KEY, Options.class)) + .setLimit(BaseResourceExpander.getLimit(expandOptions)); - if (expandOptions.containsKey("limit")) { - req.setLimit(expandOptions.get("limit", Integer.class)); - } - refSet.setMembers(req.build().execute(context())); } }