Skip to content

Commit

Permalink
Merge pull request #200 from muehmar/184-make-warnings-configurable-g…
Browse files Browse the repository at this point in the history
…lobally

Make warnings configurable globally
  • Loading branch information
muehmar authored Dec 4, 2023
2 parents 357cde6 + 9b3e149 commit 593574a
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class OpenApiSchemaExtension implements Serializable {
private final List<ConstantSchemaNameMapping> constantSchemaNameMappings;
private final GetterSuffixes getterSuffixes;
private final ValidationMethods validationMethods;
private final WarningsConfig warnings;

@Inject
public OpenApiSchemaExtension(ObjectFactory objectFactory) {
Expand All @@ -31,6 +32,7 @@ public OpenApiSchemaExtension(ObjectFactory objectFactory) {
this.constantSchemaNameMappings = new ArrayList<>();
this.getterSuffixes = GetterSuffixes.allUndefined();
this.validationMethods = ValidationMethods.allUndefined();
this.warnings = WarningsConfig.allUndefined();
}

public void schemas(Closure<SingleSchemaExtension> closure) {
Expand Down Expand Up @@ -68,6 +70,10 @@ public void constantSchemaNameMapping(Action<ConstantSchemaNameMapping> action)
constantSchemaNameMappings.add(constantSchemaNameMapping);
}

public void warnings(Action<WarningsConfig> action) {
action.execute(warnings);
}

private Optional<EnumDescriptionExtension> getCommonEnumDescription() {
return Optional.ofNullable(enumDescriptionExtension);
}
Expand All @@ -92,15 +98,19 @@ private List<ConstantSchemaNameMapping> getCommonConstantSchemaNameMappings() {
return constantSchemaNameMappings;
}

public WarningsConfig getCommonWarnings() {
return warnings;
}

public PList<SingleSchemaExtension> getSchemaExtensions() {
return PList.fromIter(schemaExtensions)
.map(ext -> ext.withCommonClassMappings(getCommonClassMappings()))
.map(ext -> ext.withCommonFormatTypeMappings(getCommonFormatTypeMappings()))
.map(ext -> ext.withCommonEnumDescription(getCommonEnumDescription()))
.map(ext -> ext.withCommonGetterSuffixes(getCommonGetterSuffixes()))
.map(ext -> ext.withCommonValidationMethods(getCommonValidationMethods()))
.map(
ext -> ext.withCommonConstantSchemaNameMappings(getCommonConstantSchemaNameMappings()));
.map(ext -> ext.withCommonConstantSchemaNameMappings(getCommonConstantSchemaNameMappings()))
.map(ext -> ext.withCommonWarnings(getCommonWarnings()));
}

@Override
Expand All @@ -120,6 +130,8 @@ public String toString() {
+ getterSuffixes
+ ", validationMethods="
+ validationMethods
+ ", warnings="
+ warnings
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ public SingleSchemaExtension withCommonConstantSchemaNameMappings(
return this;
}

public SingleSchemaExtension withCommonWarnings(WarningsConfig commonWarnings) {
this.warnings = this.warnings.withCommonWarnings(commonWarnings);
return this;
}

public PojoNameMappings getPojoNameMappings() {
return new PojoNameMappings(
PList.fromIter(constantSchemaNameMappings)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package com.github.muehmar.gradle.openapi.dsl;

import static com.github.muehmar.gradle.openapi.dsl.WarningsConfigBuilder.fullWarningsConfigBuilder;

import ch.bluecare.commons.data.PList;
import com.github.muehmar.gradle.openapi.util.Optionals;
import com.github.muehmar.gradle.openapi.warnings.FailingWarningTypes;
import com.github.muehmar.gradle.openapi.warnings.WarningType;
import io.github.muehmar.pojobuilder.annotations.Nullable;
import io.github.muehmar.pojobuilder.annotations.PojoBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.EqualsAndHashCode;
import lombok.Setter;
import lombok.ToString;

@PojoBuilder
@Setter
@EqualsAndHashCode
@ToString
public class WarningsConfig implements Serializable {
Expand All @@ -33,6 +40,23 @@ public static WarningsConfig allUndefined() {
return new WarningsConfig();
}

public WarningsConfig withCommonWarnings(WarningsConfig commonWarnings) {
return fullWarningsConfigBuilder()
.disableWarnings(
Optionals.or(
Optional.ofNullable(disableWarnings),
Optional.ofNullable(commonWarnings.disableWarnings)))
.failOnWarnings(
Optionals.or(
Optional.ofNullable(failOnWarnings),
Optional.ofNullable(commonWarnings.failOnWarnings)))
.failOnUnsupportedValidation(
Optionals.or(
Optional.ofNullable(failOnUnsupportedValidation),
Optional.ofNullable(commonWarnings.failOnUnsupportedValidation)))
.build();
}

public boolean getDisableWarnings() {
return Optional.ofNullable(disableWarnings).orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.muehmar.gradle.openapi.dsl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import ch.bluecare.commons.data.PList;
import com.github.muehmar.gradle.openapi.generator.settings.PojoNameMappings;
Expand Down Expand Up @@ -156,4 +158,29 @@ void withCommonConstantSchemaNameMappings_when_oneAdditionalMappingPresent_then_
.toArrayList(),
pojoNameMappings.getConstantNameMappings());
}

@Test
void withCommonWarnings_when_oneAdditionalMappingPresent_then_concatenated() {
final SingleSchemaExtension extension = new SingleSchemaExtension("apiV1");
final Action<WarningsConfig> warningsConfigAction =
config -> {
config.setDisableWarnings(false);
config.setFailOnUnsupportedValidation(false);
};
extension.warnings(warningsConfigAction);

final WarningsConfig commonWarnings = new WarningsConfig();
commonWarnings.setDisableWarnings(true);
commonWarnings.setFailOnWarnings(true);
commonWarnings.setFailOnUnsupportedValidation(true);

// method call
extension.withCommonWarnings(commonWarnings);

final WarningsConfig resultingWarnings = extension.getWarnings();

assertFalse(resultingWarnings.getDisableWarnings());
assertTrue(resultingWarnings.getFailOnWarnings());
assertFalse(resultingWarnings.getFailOnUnsupportedValidation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.muehmar.gradle.openapi.dsl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class WarningsConfigTest {

@ParameterizedTest
@MethodSource("commonAndExplicitFlags")
void withCommonWarnings_when_disableWarningsFlag_then_matchExpected(
Boolean common, Boolean explicit, Boolean expected) {
final WarningsConfig commonWarnings = new WarningsConfig(common, false, false);

final WarningsConfig explicitWarnings = new WarningsConfig(explicit, false, false);

final WarningsConfig resultingWarnings = explicitWarnings.withCommonWarnings(commonWarnings);

assertEquals(expected, resultingWarnings.getDisableWarnings());
}

@ParameterizedTest
@MethodSource("commonAndExplicitFlags")
void withCommonWarnings_when_failOnWarningsFlag_then_matchExpected(
Boolean common, Boolean explicit, Boolean expected) {
final WarningsConfig commonWarnings = new WarningsConfig(false, common, false);

final WarningsConfig explicitWarnings = new WarningsConfig(false, explicit, false);

final WarningsConfig resultingWarnings = explicitWarnings.withCommonWarnings(commonWarnings);

assertEquals(expected, resultingWarnings.getFailOnWarnings());
}

@ParameterizedTest
@MethodSource("commonAndExplicitFlags")
void withCommonWarnings_when_failOnUnsupportedValidationFlag_then_matchExpected(
Boolean common, Boolean explicit, Boolean expected) {
final WarningsConfig commonWarnings = new WarningsConfig(false, false, common);

final WarningsConfig explicitWarnings = new WarningsConfig(false, false, explicit);

final WarningsConfig resultingWarnings = explicitWarnings.withCommonWarnings(commonWarnings);

assertEquals(expected, resultingWarnings.getFailOnUnsupportedValidation());
}

private static Stream<Arguments> commonAndExplicitFlags() {
return Stream.of(
arguments(null, false, false),
arguments(null, true, true),
arguments(null, null, false),
arguments(false, false, false),
arguments(false, true, true),
arguments(false, null, false),
arguments(true, false, false),
arguments(true, true, true),
arguments(true, null, true));
}
}

0 comments on commit 593574a

Please sign in to comment.