--- kind: CONCEPT slug: messaging-schema-protobuf-c03 title: 어긋난 짝을 표현할 수 없게 만든 값 하나 topic: delivery-and-settlement-models project: clean-architecture-backend-template status: 게시 전 sourceRevision: 21234e38cdb9a926cbc92bb97a2aee2e4a7d2916 rootTreeNode: concept:messaging-schema-protobuf-c03 evidenceCapturedOn: 2026-09-01 assets: - key: messaging-schema-protobuf-c03 file: ../../../final/evidence/rendered/messaging-schema-protobuf-c03.svg - key: messaging-schema-protobuf-c03-diagram file: ../../../final/assets/diagrams/messaging-schema-protobuf-c03.svg evidence: - ../../../final/evidence/raw/messaging-schema-protobuf-c03.txt source: - 원본 분석 절은 final/document.md#a19-messaging-schema-protobuf#L112 이다. module: messaging-schema-protobuf --- # 어긋난 짝을 표현할 수 없게 만든 값 하나 이 leaf에서 가장 밀도 높은 결정이다. 증명 방법이 영리하다. ## 관계 - **검증되지 않는 스키마 파일은 문서임을 파일 안에 적는다** 같은 분석 리프에서 끌어낸 규칙이다. - **신뢰할 수 없는 입력 쪽 경계를 먼저 테스트한다** 같은 분석 리프에서 끌어낸 규칙이다. ## 본문 이 leaf에서 가장 밀도 높은 결정이다. 예전에는 클래스와 parser가 두 개의 평행한 맵에 살았고 둘이 일치하는지 아무도 확인하지 않아, `OrderCreated.class`와 `OrderCancelled`의 parser를 짝지은 registry가 생성 시점에 받아들여진 뒤 디코딩 시점에 브로커 스레드에서 `ClassCastException`을 냈다. 둘을 한 값에 묶으면 어긋남을 표현할 수 없다. ## 짝을 생성 시점에 증명하는 방법 :::evidence key="messaging-schema-protobuf-c03-diagram" alt="빈 바이트 파싱에서 default instance 로 proto3 유효가 건너가고 default instance 에서 선언 클래스 대조로 산출 타입이 건너간다" caption="짝을 생성 시점에 증명하는 방법" zoom="false" ::: 증명 방법이 영리하다. proto3에서 모든 필드가 wire상 optional이므로 **빈 바이트는 항상 유효한 메시지**다. 그것을 파싱하면 default instance가 나오고 그 클래스가 곧 parser의 산출 타입이다. 별도 리플렉션 없이 짝을 확인한다. ## 에러 메시지가 실패 지점을 명시한다 "a mismatched pairing fails at decode time on a broker thread, not here" — 즉 **여기서 실패하는 것이 목적**임을 메시지가 스스로 말한다. 두 코드가 다르다 — `PROTOBUF_CONTRACT_UNUSABLE`(파싱 자체 실패)과 `PROTOBUF_CONTRACT_MISMATCH`(파싱은 되는데 타입이 다름). 카테고리는 둘 다 `CONFIGURATION`이다. 테스트가 이 성질을 붙든다 — `aParserThatDoesNotProduceTheDeclaredClassIsRejectedAtConstruction`, `as("the mismatch used to surface as a ClassCastException on a broker thread")`. ## BoundedByteSink 참조 위치 :::evidence key="messaging-schema-protobuf-c03" alt="코드베이스에서 BoundedByteSink 를 검색한 출력 15줄. 이 기록이 세는 참조가 그 출력에 그대로 보인다." caption="BoundedByteSink 코드베이스 검색 — 15줄 · exit 0" zoom="true" ::: ## 세 codec 중 유일하게 사전 거절이 가능하다 `BoundedByteSink.requireFits`가 이 leaf를 위해 존재하고, schema-api의 javadoc이 그것을 명시한다 — "Protobuf knows its serialized size exactly, so the whole encode can be refused before the first byte is written." 그리고 사전 검사가 사후 경계를 대체하지 않는다 — `writeTo(sink)`가 여전히 sink를 통과하므로 이중 방어다. schema-api javadoc: "this is a cheaper refusal, not a replacement for the bound." ## 두 검사를 함께 보는 이유 `Message`인지와 등록된 클래스의 인스턴스인지를 함께 본다. 후자만으로 충분해 보이지만 전자가 `writeTo`를 부를 수 있음을 보장한다. JSON codec과 같은 비대칭이다.