|
| 1 | +# CLAUDE.md - MongoDB Java Driver |
| 2 | + |
| 3 | +Guidelines for AI agents working on the MongoDB Java Driver codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is the official MongoDB JVM driver monorepo, providing Java, Kotlin, and Scala |
| 8 | +drivers for MongoDB. The driver implements the |
| 9 | +[MongoDB Driver Specifications](https://github.com/mongodb/specifications) |
| 10 | +and follows semantic versioning. |
| 11 | + |
| 12 | +### Module Structure |
| 13 | + |
| 14 | +| Module | Purpose | |
| 15 | +|----------------------------|-----------------------------------------------------------| |
| 16 | +| `bson` | BSON library (core serialization) | |
| 17 | +| `bson-kotlin` | Kotlin BSON extensions | |
| 18 | +| `bson-kotlinx` | Kotlin serialization BSON codec | |
| 19 | +| `bson-record-codec` | Java record codec support | |
| 20 | +| `bson-scala` | Scala BSON extensions | |
| 21 | +| `driver-core` | Core driver internals (connections, protocol, operations) | |
| 22 | +| `driver-sync` | Synchronous Java driver | |
| 23 | +| `driver-legacy` | Legacy MongoDB Java driver API | |
| 24 | +| `driver-reactive-streams` | Reactive Streams driver | |
| 25 | +| `driver-kotlin-coroutine` | Kotlin Coroutines driver | |
| 26 | +| `driver-kotlin-extensions` | Kotlin driver extensions | |
| 27 | +| `driver-kotlin-sync` | Kotlin synchronous driver | |
| 28 | +| `driver-scala` | Scala driver | |
| 29 | +| `mongodb-crypt` | Client-side field-level encryption | |
| 30 | +| `testing` | Shared test resources and MongoDB specifications | |
| 31 | + |
| 32 | +### Internal API Convention |
| 33 | + |
| 34 | +All code in `com.mongodb.internal.*` and `org.bson.internal.*` is private API. |
| 35 | +It can change at any time without notice. Never expose internal types in |
| 36 | +public APIs, and never advise users to depend on them. |
| 37 | + |
| 38 | +### API Stability Annotations |
| 39 | + |
| 40 | +- `@Alpha` - Early development, may be removed. Not for production use. |
| 41 | +- `@Beta` - Subject to change or removal. Libraries should not depend on these. |
| 42 | +- `@Evolving` - May add abstract methods in future releases. Safe to use, but implementing/extending bears upgrade risk. |
| 43 | +- `@Sealed` - Must not be extended or implemented by consumers. Safe to use, but not to subclass. |
| 44 | +- `@Deprecated` - Supported until next major release but should be migrated away from. |
| 45 | + |
| 46 | +## General |
| 47 | + |
| 48 | +- Follow existing conventions. Keep it simple. |
| 49 | +- When stuck: stop, explain the problem, propose alternatives, ask for guidance. |
| 50 | +- When uncertain: ask rather than assume. Present options with trade-offs. |
| 51 | + |
| 52 | +## Build System |
| 53 | + |
| 54 | +- **Build tool:** Gradle with Kotlin DSL |
| 55 | +- **Build JDK:** Java 17+ |
| 56 | +- **Source compatibility (baseline):** Java 8 for most modules / main driver artifacts |
| 57 | +- **Version catalog:** `gradle/libs.versions.toml` |
| 58 | +- **Convention plugins:** `buildSrc/src/main/kotlin/conventions/` |
| 59 | + |
| 60 | +### Essential Build Commands |
| 61 | + |
| 62 | +```bash |
| 63 | +# Full validation (static checks + unit tests + integration tests) |
| 64 | +./gradlew check |
| 65 | + |
| 66 | +# Integration tests (requires running MongoDB) |
| 67 | +./gradlew integrationTest -Dorg.mongodb.test.uri="mongodb://localhost:27017" |
| 68 | + |
| 69 | +# Single module tests |
| 70 | +./gradlew :driver-core:test |
| 71 | + |
| 72 | +# Format check only |
| 73 | +./gradlew spotlessCheck |
| 74 | + |
| 75 | +# Auto-fix formatting |
| 76 | +./gradlew spotlessApply |
| 77 | + |
| 78 | +# Test with alternative JDK |
| 79 | +./gradlew test -PjavaVersion=11 |
| 80 | +``` |
| 81 | + |
| 82 | +## Code Style and Formatting |
| 83 | + |
| 84 | +Formatting is enforced automatically because the Gradle `check` task depends on |
| 85 | +`./gradlew spotlessApply`, which formats sources. To run a check without modifying |
| 86 | +files, invoke `./gradlew spotlessCheck` manually. Do not manually reformat files |
| 87 | +outside the scope of your changes. |
| 88 | + |
| 89 | +### Style Rules |
| 90 | + |
| 91 | +- **Max line length:** 140 characters |
| 92 | +- **Indentation:** 4 spaces (no tabs) |
| 93 | +- **Line endings:** LF (Unix) |
| 94 | +- **Charset:** UTF-8 |
| 95 | +- **Star imports:** Prohibited (AvoidStarImport) |
| 96 | +- **Final parameters:** Required (FinalParameters checkstyle rule) |
| 97 | +- **Braces:** Required for all control structures (NeedBraces) |
| 98 | +- **Else placement:** On its own line (not cuddled) |
| 99 | +- **Copyright header:** Every Java / Kotlin / Scala file must contain `Copyright 2008-present MongoDB, Inc.` |
| 100 | + |
| 101 | +### Prohibited Patterns |
| 102 | + |
| 103 | +- `System.out.println` / `System.err.println` — Use SLF4J logging |
| 104 | +- `e.printStackTrace()` — Use proper logging/error handling |
| 105 | + |
| 106 | +## Testing |
| 107 | + |
| 108 | +### Frameworks |
| 109 | + |
| 110 | +- **JUnit 5** (Jupiter) - Primary unit test framework |
| 111 | +- **Spock** (Groovy) - Legacy, do not add new Spock tests. |
| 112 | +- **Mockito** - Mocking |
| 113 | +- **ScalaTest** - Scala module testing |
| 114 | + |
| 115 | +### Writing Tests |
| 116 | + |
| 117 | +- **Every code change must include tests.** New code needs new tests; modified code needs updated tests. Do not reduce test coverage. |
| 118 | +- Extend existing test patterns in the module you are modifying |
| 119 | +- Unit tests should not require a running MongoDB instance |
| 120 | +- Test methods should be descriptive: |
| 121 | + prefer `shouldReturnEmptyListWhenNoDocumentsMatch()` over `test1()`, |
| 122 | + use `@DisplayName` for a human readable name |
| 123 | +- Clean up test data in `@AfterEach` / `cleanup()` to prevent test pollution |
| 124 | + |
| 125 | +### MongoDB Specifications and Specification Tests |
| 126 | + |
| 127 | +The driver implements the [MongoDB Specifications](https://github.com/mongodb/specifications). Specification test data files live in |
| 128 | +`testing/resources/specifications/` — a git submodule for test resources. When modifying driver specification-related behavior: do |
| 129 | +not modify existing specification tests unless they test incorrect behavior. Create new tests instead. |
| 130 | + |
| 131 | +## Core Development Principles |
| 132 | + |
| 133 | +### Essential Rules |
| 134 | + |
| 135 | +- **Read before modifying.** Understand existing code and patterns before making changes. |
| 136 | +- **Minimal changes only.** No drive-by refactoring. |
| 137 | +- **Preserve existing comments.** Only remove comments that are provably incorrect. |
| 138 | +- **No unrelated changes.** Do not fix formatting or refactor code outside the scope of the task. |
| 139 | +- **No rewrites** without explicit permission. |
| 140 | + |
| 141 | +### Search Before Implementing |
| 142 | + |
| 143 | +Before writing new code, search the codebase for existing implementations: |
| 144 | + |
| 145 | +- Check if a utility method already exists in `com.mongodb.internal.*` |
| 146 | +- Check if a similar pattern is established elsewhere in the module |
| 147 | +- Reuse existing well-tested infrastructure over creating duplicates |
| 148 | + |
| 149 | +### API Design |
| 150 | + |
| 151 | +- **Deep modules:** Prefer simple interfaces with powerful implementations over shallow wrappers. |
| 152 | +- **Information hiding:** Bury complexity behind simple interfaces. |
| 153 | +- **Pull complexity downward:** Make the implementer work harder so callers work less. |
| 154 | +- **General-purpose over special-case:** Fewer flexible methods over many specialized ones. |
| 155 | +- **Define errors out of existence:** Design APIs so errors cannot happen rather than detecting and handling them. |
| 156 | + |
| 157 | +## Dependency Management |
| 158 | + |
| 159 | +Dependencies are managed centrally via `gradle/libs.versions.toml`. |
| 160 | + |
| 161 | +- **Never add dependencies without justification.** Dependencies are liabilities. |
| 162 | +- **Use dependencies for:** Security/crypto, complex protocols, battle-tested algorithms. |
| 163 | +- **Write it yourself for:** fewer than 100 LOC of straightforward code, project-specific logic, performance-critical paths. |
| 164 | +- Optional dependencies (Netty, Snappy, Zstd, AWS SDK, SLF4J) use `optionalImplementation` and must not be required at runtime. |
| 165 | + |
| 166 | +## Safety Rules - Do Not Modify Without Review |
| 167 | + |
| 168 | +- Wire protocol implementation (connection/authentication handshakes) |
| 169 | +- Security-critical authentication and encryption code |
| 170 | +- Public API contracts (breaking changes require major version bump) |
| 171 | +- BSON specification compliance |
| 172 | +- Configuration files containing credentials or secrets |
| 173 | +- CI/CD pipeline configurations in `.evergreen/` |
| 174 | +- Release and publishing scripts |
| 175 | + |
| 176 | +## CI/CD |
| 177 | + |
| 178 | +### Evergreen (MongoDB Internal CI) |
| 179 | + |
| 180 | +Primary CI runs on MongoDB's Evergreen system. Configuration is in `.evergreen/`. |
| 181 | + |
| 182 | +### Before Submitting |
| 183 | + |
| 184 | +Run locally at minimum: |
| 185 | + |
| 186 | +```bash |
| 187 | +./gradlew doc check scalaCheck # Generates docs, runs static checks + tests |
| 188 | +``` |
0 commit comments