Skip to content

Commit

Permalink
fix: enum 상수 추론을 위한 field 추가 및 파서 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Aug 13, 2024
1 parent 9d0d680 commit 125c96e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
public @interface ApiExceptionExplanation {
Class<? extends BaseErrorCode> value();

/**
* BaseErrorCode를 구현한 Enum 클래스의 상수명
*/
String constant();

String name() default "";

String mediaType() default "application/json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ private static void addExamplesToResponses(ApiResponses responses, Map<Integer,
@Builder(access = AccessLevel.PRIVATE)
private record ExampleHolder(int httpStatus, String name, String mediaType, String description, Example holder) {
static ExampleHolder from(ApiExceptionExplanation annotation) {
if (annotation instanceof BaseErrorCode errorCode) {
return ExampleHolder.builder()
.httpStatus(errorCode.causedBy().statusCode().getCode())
.name(StringUtils.hasText(annotation.name()) ? annotation.name() : errorCode.getExplainError())
.mediaType(annotation.mediaType())
.description(annotation.description())
.holder(createExample(errorCode, annotation.summary(), annotation.description()))
.build();
} else {
throw new IllegalArgumentException("Annotation must be an instance of BaseErrorCode");
}
BaseErrorCode errorCode = getErrorCode(annotation);

return ExampleHolder.builder()
.httpStatus(errorCode.causedBy().statusCode().getCode())
.name(StringUtils.hasText(annotation.name()) ? annotation.name() : errorCode.getExplainError())
.mediaType(annotation.mediaType())
.description(annotation.description())
.holder(createExample(errorCode, annotation.summary(), annotation.description()))
.build();
}

@SuppressWarnings("unchecked")
public static <E extends Enum<E> & BaseErrorCode> E getErrorCode(ApiExceptionExplanation annotation) {
Class<E> enumClass = (Class<E>) annotation.value();
return Enum.valueOf(enumClass, annotation.constant());
}

private static Example createExample(BaseErrorCode errorCode, String summary, String description) {
Expand Down

0 comments on commit 125c96e

Please sign in to comment.