Bird
0
0

You want to document a complex API model field that can be null but has a maximum length of 50 characters. Which @Schema annotation usage is correct?

hard📝 Application Q15 of 15
Spring Boot - API Documentation
You want to document a complex API model field that can be null but has a maximum length of 50 characters. Which @Schema annotation usage is correct?
A@Schema(description = "Optional comment", maxLength = 50, nullable = false) private String comment;
B@Schema(description = "Optional comment", maxLength = "50", nullable = "true") private String comment;
C@Schema(description = "Optional comment", maxLength = 50, required = true) private String comment;
D@Schema(description = "Optional comment", maxLength = 50, nullable = true) private String comment;
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute types for @Schema

    Attributes like maxLength expect an integer, and nullable expects a boolean.
  2. Step 2: Match requirements with options

    The field can be null, so nullable = true is needed, and maxLength should be 50 as an integer.
  3. Step 3: Eliminate incorrect options

    @Schema(description = "Optional comment", maxLength = "50", nullable = "true") private String comment; uses strings for boolean and integer, which is wrong. @Schema(description = "Optional comment", maxLength = 50, required = true) private String comment; marks it required (not nullable). @Schema(description = "Optional comment", maxLength = 50, nullable = false) private String comment; sets nullable false, disallowing null.
  4. Final Answer:

    @Schema(description = "Optional comment", maxLength = 50, nullable = true) private String comment; -> Option D
  5. Quick Check:

    Use correct types and nullable=true for optional fields = A [OK]
Quick Trick: Use boolean and int types correctly in @Schema attributes [OK]
Common Mistakes:
  • Using strings instead of boolean or int for attributes
  • Confusing nullable and required attributes
  • Setting nullable false when field can be null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes