Skip to content

Commit

Permalink
fix: using stable ordering for deprecated metadata
Browse files Browse the repository at this point in the history
closes: keycloak#34858

Signed-off-by: Steve Hawkins <[email protected]>
(cherry picked from commit 245498c)
  • Loading branch information
shawkins authored Nov 25, 2024
1 parent 3a9cc8e commit 69001b3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,36 @@

package org.keycloak.config;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
* @author Vaclav Muzikar <[email protected]>
*/
public class DeprecatedMetadata {
private final Set<String> newOptionsKeys;
private final List<String> newOptionsKeys;
private final String note;
private final Set<String> deprecatedValues;

private DeprecatedMetadata(Set<String> newOptionsKeys, String note, Set<String> deprecatedValues) {
this.newOptionsKeys = newOptionsKeys == null ? Collections.emptySet() : Collections.unmodifiableSet(newOptionsKeys);
private DeprecatedMetadata(List<String> newOptionsKeys, String note, Set<String> deprecatedValues) {
this.newOptionsKeys = newOptionsKeys;
this.note = note;
this.deprecatedValues = deprecatedValues == null ? Collections.emptySet() : Collections.unmodifiableSet(deprecatedValues);
this.deprecatedValues = deprecatedValues;
}

public static DeprecatedMetadata deprecateOption(String note, Set<String> newOptionsKeys) {
return new DeprecatedMetadata(newOptionsKeys, note, null);
public static DeprecatedMetadata deprecateOption(String note, String... newOptionsKeys) {
return new DeprecatedMetadata(Arrays.asList(newOptionsKeys), note, Set.of());
}

public static DeprecatedMetadata deprecateValues(Set<String> values, String note) {
return new DeprecatedMetadata(null, note, values);
public static DeprecatedMetadata deprecateValues(String note, String... values) {
return new DeprecatedMetadata(Collections.emptyList(), note,
Collections.unmodifiableSet(new LinkedHashSet<String>(Arrays.asList(values))));
}

public Set<String> getNewOptionsKeys() {
public List<String> getNewOptionsKeys() {
return newOptionsKeys;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -119,31 +118,20 @@ public OptionBuilder<T> expectedValues(boolean strict, T... expected) {
}

public OptionBuilder<T> deprecated() {
this.deprecatedMetadata = DeprecatedMetadata.deprecateOption(null, null);
this.deprecatedMetadata = DeprecatedMetadata.deprecateOption(null);
return this;
}

public OptionBuilder<T> deprecated(String note) {
this.deprecatedMetadata = DeprecatedMetadata.deprecateOption(note, null);
public OptionBuilder<T> deprecatedMetadata(DeprecatedMetadata deprecatedMetadata) {
this.deprecatedMetadata = deprecatedMetadata;
return this;
}

public OptionBuilder<T> deprecated(Set<String> newOptionsKeys) {
this.deprecatedMetadata = DeprecatedMetadata.deprecateOption(null, newOptionsKeys);
public OptionBuilder<T> deprecatedValues(String note, T... values) {
this.deprecatedMetadata = DeprecatedMetadata.deprecateValues(note, Stream.of(values).map(Object::toString).toArray(String[]::new));
return this;
}

public OptionBuilder<T> deprecated(String note, Set<String> newOptionsKeys) {
this.deprecatedMetadata = DeprecatedMetadata.deprecateOption(note, newOptionsKeys);
return this;
}

public OptionBuilder<T> deprecatedValues(Set<String> values, String note) {
this.deprecatedMetadata = DeprecatedMetadata.deprecateValues(values, note);
return this;
}


public Option<T> build() {
if (deprecatedMetadata == null && category.getSupportLevel() == ConfigSupportLevel.DEPRECATED) {
deprecated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.keycloak.common.enums.HostnameVerificationPolicy;

import java.util.List;
import java.util.Set;

public class TruststoreOptions {

Expand All @@ -16,7 +15,7 @@ public class TruststoreOptions {
.category(OptionCategory.TRUSTSTORE)
.description("The TLS hostname verification policy for out-going HTTPS and SMTP requests.")
.defaultValue(HostnameVerificationPolicy.DEFAULT)
.deprecatedValues(Set.of("STRICT", "WILDCARD"), "STRICT and WILDCARD have been deprecated, use DEFAULT instead.")
.deprecatedValues("STRICT and WILDCARD have been deprecated, use DEFAULT instead.", HostnameVerificationPolicy.STRICT, HostnameVerificationPolicy.WILDCARD)
.build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ private static String getDecoratedOptionDescription(PropertyMapper<?> mapper) {

if (mapper.getType() != Boolean.class && !mapper.getExpectedValues().isEmpty()) {
List<String> decoratedExpectedValues = mapper.getExpectedValues().stream().map(value -> {
if (mapper.getDeprecatedMetadata().isPresent() && mapper.getDeprecatedMetadata().get().getDeprecatedValues().contains(value)) {
if (mapper.getDeprecatedMetadata().filter(metadata -> metadata.getDeprecatedValues().contains(value)).isPresent()) {
return value + " (deprecated)";
}
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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
*
* http://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.keycloak.config;

import static org.junit.Assert.assertArrayEquals;

import org.junit.Test;

public class OptionBuilderTest {

@Test
public void testDeprecatedOptionOrdering() {
String[] values = new String[] {"a", "d", "b", "1"};
var option = new OptionBuilder<>("key", String.class).deprecatedValues("These are deprecated", values).build();
assertArrayEquals(option.getDeprecatedMetadata().get().getDeprecatedValues().toArray(String[]::new), values);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package or.keycloak.quarkus.runtime.cli;
package org.keycloak.quarkus.runtime.cli;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -28,7 +28,6 @@
import java.util.List;

import org.junit.Test;
import org.keycloak.quarkus.runtime.cli.Picocli;
import org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand;
import org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource;
import org.keycloak.quarkus.runtime.configuration.test.AbstractConfigurationTest;
Expand Down

0 comments on commit 69001b3

Please sign in to comment.