Skip to content

Commit

Permalink
chore(refactor): fix spotbugs warnings for dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Oct 29, 2023
1 parent 5c65efa commit 1ed5560
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.tbk.bitcoin.exchange.example.api.rate.ExchangeRateResponseImpl.ExchangeRateImpl;
import org.tbk.bitcoin.exchange.example.api.rate.ExchangeRateResponseDtoImpl.ExchangeRateDtoImpl;
import reactor.core.publisher.Flux;

import javax.money.CurrencyUnit;
Expand Down Expand Up @@ -43,7 +43,7 @@ public ResponseEntity<? extends Map<String, Object>> get() {
}

@GetMapping("/latest")
public ResponseEntity<ExchangeRateResponse> latest(
public ResponseEntity<ExchangeRateResponseDto> latest(
@RequestParam(name = "base", required = false) CurrencyUnit baseParamOrNull,
@Parameter(array = @ArraySchema(schema = @Schema(implementation = String.class)))
@RequestParam(name = "target", required = false) List<CurrencyUnit> targetParamOrNull,
Expand All @@ -64,7 +64,7 @@ public ResponseEntity<ExchangeRateResponse> latest(
ConversionQueryBuilder conversionQueryBuilder = ConversionQueryBuilder.of()
.setBaseCurrency(baseCurrency);

Optional<ExchangeRateResponse> exchangeRateResponse = Flux.fromIterable(targetCurrencies)
Optional<ExchangeRateResponseDto> exchangeRateResponse = Flux.fromIterable(targetCurrencies)
.map(conversionQueryBuilder::setTermCurrency)
.map(ConversionQueryBuilder::build)
.flatMap(conversionQuery -> {
Expand All @@ -87,9 +87,9 @@ public ResponseEntity<ExchangeRateResponse> latest(
}
});
})
.map(exchangeRate -> ExchangeRateImpl.toDto(exchangeRate).build())
.map(exchangeRate -> ExchangeRateDtoImpl.toDto(exchangeRate).build())
.collectList()
.map(rates -> (ExchangeRateResponse) ExchangeRateResponseImpl.builder()
.map(rates -> (ExchangeRateResponseDto) ExchangeRateResponseDtoImpl.builder()
.base(baseCurrency.getCurrencyCode())
.rates(rates)
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.util.List;
import java.util.Map;

@JsonDeserialize(as = ExchangeRateResponseImpl.ExchangeRateImpl.class)
public interface ExchangeRate {
@JsonDeserialize(as = ExchangeRateResponseDtoImpl.ExchangeRateDtoImpl.class)
public interface ExchangeRateDto {
String getBase();

boolean isDerived();
Expand All @@ -20,7 +20,7 @@ public interface ExchangeRate {

String getType();

List<ExchangeRate> getChain();
List<ExchangeRateDto> getChain();

Map<String, Object> getMeta();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import java.util.List;

@JsonDeserialize(as = ExchangeRateResponseImpl.class)
public interface ExchangeRateResponse {
@JsonDeserialize(as = ExchangeRateResponseDtoImpl.class)
public interface ExchangeRateResponseDto {
String getBase();

List<? extends ExchangeRate> getRates();
List<? extends ExchangeRateDto> getRates();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
@Value
@Builder
@Jacksonized
public class ExchangeRateResponseImpl implements ExchangeRateResponse {
public class ExchangeRateResponseDtoImpl implements ExchangeRateResponseDto {

String base;
List<? extends ExchangeRate> rates;
List<? extends ExchangeRateDto> rates;

@Value
@Builder
@Jacksonized
public static class ExchangeRateImpl implements ExchangeRate {
public static class ExchangeRateDtoImpl implements ExchangeRateDto {

private static final class ContextUtil {
public static Map<String, Object> toMap(AbstractContext context) {
Expand All @@ -34,8 +34,8 @@ public static Map<String, Object> toMap(AbstractContext context) {
}
}

public static ExchangeRateImplBuilder toDto(javax.money.convert.ExchangeRate exchangeRate) {
return ExchangeRateResponseImpl.ExchangeRateImpl.builder()
public static ExchangeRateDtoImplBuilder toDto(javax.money.convert.ExchangeRate exchangeRate) {
return ExchangeRateDtoImpl.builder()
.factor(new BigDecimal(exchangeRate.getFactor().toString()))
.base(exchangeRate.getBaseCurrency().getCurrencyCode())
.target(exchangeRate.getCurrency().getCurrencyCode())
Expand All @@ -44,8 +44,8 @@ public static ExchangeRateImplBuilder toDto(javax.money.convert.ExchangeRate exc
.type(exchangeRate.getContext().getRateType().name())
.chain(exchangeRate.getExchangeRateChain().stream()
.filter(val -> !exchangeRate.equals(val))
.map(ExchangeRateImpl::toDto)
.map(ExchangeRateImplBuilder::build)
.map(ExchangeRateDtoImpl::toDto)
.map(ExchangeRateDtoImplBuilder::build)
.toList())
.meta(ContextUtil.toMap(exchangeRate.getContext()))
.date(exchangeRate.getContext().get(LocalDate.class));
Expand All @@ -65,7 +65,7 @@ public static ExchangeRateImplBuilder toDto(javax.money.convert.ExchangeRate exc
LocalDate date;

@Singular("addChain")
List<ExchangeRate> chain;
List<ExchangeRateDto> chain;

@Singular("addMeta")
Map<String, Object> meta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tbk.lightning.playground.example.api;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Value;
Expand Down Expand Up @@ -27,6 +28,7 @@
@Schema(
name = "ApiError"
)
@SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}, justification = "on purpose")
public class ErrorAttributesSchema {
@Schema(example = "2019-01-17T16:12:45.977+0000")
String timestamp;
Expand Down
4 changes: 2 additions & 2 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
<Match>
<!-- disable some known risks or minor warnings in example apps dto classes -->
<Or>
<Class name="~org\.tbk\..*\.example\..*Dto"/>
<Class name="~org\.tbk\..*\.example\..*DtoBuilder"/>
<Class name="~org\.tbk\..*\.example\..*Dto(Impl)?"/>
<Class name="~org\.tbk\..*\.example\..*Dto(Impl)?Builder"/>
</Or>
<Or>
<Bug pattern="EI_EXPOSE_REP"/>
Expand Down

0 comments on commit 1ed5560

Please sign in to comment.