Skip to content

Commit

Permalink
Merge pull request #316 from muehmar/312-option-to-confirm-mapping-wi…
Browse files Browse the repository at this point in the history
…thout-conversion

312 option to confirm mapping without conversion
  • Loading branch information
muehmar authored Nov 7, 2024
2 parents e25f869 + 727718b commit 62fcf12
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 26 deletions.
44 changes: 28 additions & 16 deletions doc/010_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ openApiGenerator {
formatTypeMapping {
formatType = "password"
classType = "com.package.Password"
disableMissingConversionWarning = true
}
// Additional class mapping
classMapping {
fromClass = "List"
toClass = "java.util.ArrayList"
disableMissingConversionWarning = true
}
// Additional DTO mapping
Expand Down Expand Up @@ -189,24 +191,27 @@ stagedBuilder {

The plugin allows one to map specific standard java classes, used in the DTO to custom types. The mapping is not applied
to generated DTO classes itself, this only includes the java class used for properties in the DTO. The following example
would use the custom List implementation `com.package.CustomList`
for lists instead of
`java.util.List`. The config-property
`toClass` should be the fully qualified classname to properly generate import-statements. The
`conversion.fromCustomType` and
`conversion.toCustomType` are used in the DTO to convert from and to the custom type,
see [Conversion for Mappings](#conversions-for-mappings).
would use the custom List implementation `com.package.CustomList` for lists instead of `java.util.List`.

```
classMapping {
fromClass = "List"
toClass = "com.package.CustomList"
conversion.fromCustomType = "asList"
conversion.toCustomType = "CustomList#fromList"
disableMissingConversionWarning = false
}
```

| Key | Data Type | Default | Description |
|---------------------------------|-----------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| fromClass | String | | The class used in the DTO's by the plugin which should get replaced |
| toClass | String | | Fully qualified classname used in the DTO's instead of the `fromClass` |
| conversion.fromCustomType | String | | The conversion used to convert properties of type `toClass` to `fromClass`. Possible definitions: [Conversion for Mappings](#conversions-for-mappings) |
| conversion.toCustomType | String | | The conversion used to convert properties of type `fromClass` to `toClass`. Possible definitions: [Conversion for Mappings](#conversions-for-mappings) |
| disableMissingConversionWarning | boolean | false | Set this property to true in case no warning should get emitted if no conversion is specified. This can be helpful in case the used type is designed to be used with serialisation and validation frameworks and a conversion does not make sense. |

Repeat this block for each class mapping.

### Format Type Mappings
Expand All @@ -233,10 +238,15 @@ formatTypeMapping {
}
```

will use the class `com.package.UserName` for the property `userName`. The config-property `classType` should be the
fully qualified classname to properly generate import-statements. The `conversion.fromCustomType` and
`conversion.toCustomType` are used in the DTO to convert from and to the custom type,
see [Conversion for Mappings](#conversions-for-mappings).
will use the class `com.package.UserName` for the property `userName`.

| Key | Data Type | Default | Description |
|---------------------------------|-----------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| formatType | String | | The format used in the type which should get replaced. |
| classType | String | | Fully qualified classname used in the DTO's for the types with the format `formatType`. |
| conversion.fromCustomType | String | | The conversion used to convert properties of type `classType` to the type with format `formatType`. Possible definitions: [Conversion for Mappings](#conversions-for-mappings) |
| conversion.toCustomType | String | | The conversion used to convert properties of type with format `formatType` to `classType`. Possible definitions: [Conversion for Mappings](#conversions-for-mappings) |
| disableMissingConversionWarning | boolean | false | Set this property to true in case no warning should get emitted if no conversion is specified. This can be helpful in case the used type is designed to be used with serialisation and validation frameworks and a conversion does not make sense. |

Repeat this block for each format type mapping.

Expand All @@ -253,11 +263,13 @@ dtoMapping {
}
```

The config-property `dtoName` is the full name of the generated DTO class which should get replaced by the customType
defined with `customType`. The `customType` should be the fully qualified classname to properly generate the import. The
`conversion.fromCustomType` and
`conversion.toCustomType` are used in the DTO to convert from and to the custom type,
see [Conversion for Mappings](#conversions-for-mappings).
| Key | Data Type | Default | Description |
|---------------------------------|-----------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| dtoName | String | | The name of the DTO which should get replaced. The complete name including the suffix should be used. |
| customType | String | | Fully qualified classname used instead of the class `dtoName` |
| conversion.fromCustomType | String | | The conversion used to convert properties of type `customType` to `dtoName`. Possible definitions: [Conversion for Mappings](#conversions-for-mappings) |
| conversion.toCustomType | String | | The conversion used to convert properties of type `dtoName` to `customType`. Possible definitions: [Conversion for Mappings](#conversions-for-mappings) |
| disableMissingConversionWarning | boolean | false | Set this property to true in case no warning should get emitted if no conversion is specified. This can be helpful in case the used type is designed to be used with serialisation and validation frameworks and a conversion does not make sense. |

### Conversions for mappings

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.muehmar.gradle.openapi.dsl;

import static com.github.muehmar.gradle.openapi.generator.settings.ClassTypeMappingBuilder.fullClassTypeMappingBuilder;

import com.github.muehmar.gradle.openapi.generator.settings.ClassTypeMapping;
import java.io.Serializable;
import lombok.Data;
Expand All @@ -9,13 +11,19 @@ public class ClassMapping implements Serializable {
private String fromClass;
private String toClass;
private final Conversion conversion;
private boolean disableMissingConversionWarning;

public ClassMapping() {
this.conversion = new Conversion();
}

public ClassTypeMapping toSettingsClassMapping() {
return new ClassTypeMapping(fromClass, toClass, conversion.toTypeConversion());
return fullClassTypeMappingBuilder()
.fromClass(fromClass)
.toClass(toClass)
.disableMissingConversionWarning(disableMissingConversionWarning)
.typeConversion(conversion.toTypeConversion())
.build();
}

void assertCompleteTypeConversion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.muehmar.gradle.openapi.dsl;

import static com.github.muehmar.gradle.openapi.generator.settings.DtoMappingBuilder.fullDtoMappingBuilder;

import java.io.Serializable;
import lombok.Data;

Expand All @@ -8,14 +10,19 @@ public class DtoMapping implements Serializable {
private String dtoName;
private String customType;
private final Conversion conversion;
private boolean disableMissingConversionWarning;

public DtoMapping() {
this.conversion = new Conversion();
}

public com.github.muehmar.gradle.openapi.generator.settings.DtoMapping toSettingsDtoMapping() {
return new com.github.muehmar.gradle.openapi.generator.settings.DtoMapping(
dtoName, customType, conversion.toTypeConversion());
return fullDtoMappingBuilder()
.dtoName(dtoName)
.customType(customType)
.disableMissingConversionWarning(disableMissingConversionWarning)
.typeConversion(conversion.toTypeConversion())
.build();
}

void assertCompleteTypeConversion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.muehmar.gradle.openapi.dsl;

import static com.github.muehmar.gradle.openapi.generator.settings.FormatTypeMappingBuilder.fullFormatTypeMappingBuilder;

import java.io.Serializable;
import lombok.Data;

Expand All @@ -8,15 +10,20 @@ public class FormatTypeMapping implements Serializable {
private String formatType;
private String classType;
private final Conversion conversion;
private boolean disableMissingConversionWarning;

public FormatTypeMapping() {
this.conversion = new Conversion();
}

public com.github.muehmar.gradle.openapi.generator.settings.FormatTypeMapping
toSettingsFormatTypeMapping() {
return new com.github.muehmar.gradle.openapi.generator.settings.FormatTypeMapping(
formatType, classType, conversion.toTypeConversion());
return fullFormatTypeMappingBuilder()
.formatType(formatType)
.classType(classType)
.disableMissingConversionWarning(disableMissingConversionWarning)
.typeConversion(conversion.toTypeConversion())
.build();
}

void assertCompleteTypeConversion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package com.github.muehmar.gradle.openapi.generator.settings;

import static com.github.muehmar.gradle.openapi.util.Booleans.not;

import io.github.muehmar.pojobuilder.annotations.Nullable;
import io.github.muehmar.pojobuilder.annotations.PojoBuilder;
import java.io.Serializable;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Value;

@Value
@AllArgsConstructor
@PojoBuilder
public class ClassTypeMapping implements Serializable {
String fromClass;
String toClass;
TypeConversion typeConversion;
@Nullable TypeConversion typeConversion;
boolean disableMissingConversionWarning;

public ClassTypeMapping(
String fromClass, String toClass, Optional<TypeConversion> typeConversion) {
this.fromClass = fromClass;
this.toClass = toClass;
this.typeConversion = typeConversion.orElse(null);
this.disableMissingConversionWarning = false;
}

public Optional<TypeConversion> getTypeConversion() {
return Optional.ofNullable(typeConversion);
}

public boolean isMissingConversionWarningEnabled() {
return not(disableMissingConversionWarning);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package com.github.muehmar.gradle.openapi.generator.settings;

import static com.github.muehmar.gradle.openapi.util.Booleans.not;

import io.github.muehmar.pojobuilder.annotations.Nullable;
import io.github.muehmar.pojobuilder.annotations.PojoBuilder;
import java.io.Serializable;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Value;

@Value
@AllArgsConstructor
@PojoBuilder
public class DtoMapping implements Serializable {
String dtoName;
String customType;
TypeConversion typeConversion;
@Nullable TypeConversion typeConversion;
boolean disableMissingConversionWarning;

public DtoMapping(String dtoName, String customType, Optional<TypeConversion> typeConversion) {
this.dtoName = dtoName;
this.customType = customType;
this.typeConversion = typeConversion.orElse(null);
this.disableMissingConversionWarning = false;
}

public Optional<TypeConversion> getTypeConversion() {
return Optional.ofNullable(typeConversion);
}

public boolean isMissingConversionWarningEnabled() {
return not(disableMissingConversionWarning);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package com.github.muehmar.gradle.openapi.generator.settings;

import static com.github.muehmar.gradle.openapi.util.Booleans.not;

import io.github.muehmar.pojobuilder.annotations.Nullable;
import io.github.muehmar.pojobuilder.annotations.PojoBuilder;
import java.io.Serializable;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Value;

@Value
@AllArgsConstructor
@PojoBuilder
public class FormatTypeMapping implements Serializable {
String formatType;
String classType;
TypeConversion typeConversion;
@Nullable TypeConversion typeConversion;
boolean disableMissingConversionWarning;

public FormatTypeMapping(
String formatType, String classType, Optional<TypeConversion> typeConversion) {
this.formatType = formatType;
this.classType = classType;
this.typeConversion = typeConversion.orElse(null);
this.disableMissingConversionWarning = false;
}

public Optional<TypeConversion> getTypeConversion() {
return Optional.ofNullable(typeConversion);
}

public boolean isMissingConversionWarningEnabled() {
return not(disableMissingConversionWarning);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,29 @@ public PojoNameMapping pojoNameMapping() {
public void validate() {
classTypeMappings.forEach(
classTypeMapping -> {
if (not(classTypeMapping.getTypeConversion().isPresent())) {
if (not(classTypeMapping.getTypeConversion().isPresent())
&& classTypeMapping.isMissingConversionWarningEnabled()) {
final Warning warning = Warning.missingMappingConversion(classTypeMapping);
WarningsContext.addWarningForTask(taskIdentifier, warning);
}
});

formatTypeMappings.forEach(
formatTypeMapping -> {
if (not(formatTypeMapping.getTypeConversion().isPresent())) {
if (not(formatTypeMapping.getTypeConversion().isPresent())
&& formatTypeMapping.isMissingConversionWarningEnabled()) {
final Warning warning = Warning.missingMappingConversion(formatTypeMapping);
WarningsContext.addWarningForTask(taskIdentifier, warning);
}
});

dtoMappings.forEach(
dtoMapping -> {
if (not(dtoMapping.getTypeConversion().isPresent())
&& dtoMapping.isMissingConversionWarningEnabled()) {
final Warning warning = Warning.missingMappingConversion(dtoMapping);
WarningsContext.addWarningForTask(taskIdentifier, warning);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.muehmar.gradle.openapi.generator.java.model.type.JavaType;
import com.github.muehmar.gradle.openapi.generator.java.model.validation.ConstraintType;
import com.github.muehmar.gradle.openapi.generator.settings.ClassTypeMapping;
import com.github.muehmar.gradle.openapi.generator.settings.DtoMapping;
import com.github.muehmar.gradle.openapi.generator.settings.FormatTypeMapping;
import lombok.Value;

Expand Down Expand Up @@ -39,4 +40,10 @@ public static Warning missingMappingConversion(FormatTypeMapping formatTypeMappi
formatTypeMapping.getFormatType());
return new Warning(WarningType.MISSING_MAPPING_CONVERSION, message);
}

public static Warning missingMappingConversion(DtoMapping dtoMapping) {
final String message =
String.format("DtoMapping for DTO %s has no conversion defined.", dtoMapping.getDtoName());
return new Warning(WarningType.MISSING_MAPPING_CONVERSION, message);
}
}

0 comments on commit 62fcf12

Please sign in to comment.