Prev Next

Spring / OpenAPI Interview questions

OpenAPI Interview Questions: Beginner to Advanced

Beginner Level: Fundamentals

1. What is OpenAPI, and how does it differ from Swagger?

OpenAPI is the specification (the standard) for describing RESTful APIs. Swagger refers to the tools (Swagger UI, Editor, Codegen) used to implement that spec. Since version 3.0, the specification is officially titled OpenAPI.

2. What are the primary benefits of using OpenAPI?

  • Documentation: Interactive UIs like Swagger UI.
  • Contract-First: Teams can work in parallel based on a shared contract.
  • Automation: Automatic generation of Client SDKs and Server stubs.
  • Validation: Ensures requests and responses match the defined schema.

3. What is the standard structure of an OpenAPI document?

An OpenAPI file typically contains:

  • openapi: Version of the spec.
  • info: Metadata (Title, Version).
  • servers: Connectivity info.
  • paths: Endpoints and operations.
  • components: Reusable objects.

Intermediate Level: Implementation & Design

4. Explain the components section and why it is used.

The components section stores reusable objects like schemas, security schemes, and parameters. It follows the DRY (Don't Repeat Yourself) principle, allowing you to reference these objects using $ref.

5. How do you handle path parameters vs. query parameters?

Both are defined under parameters:

  • Path: in: path (Required). Used for resource identification (e.g., /users/{id}).
  • Query: in: query. Used for filtering or pagination (e.g., /users?role=admin).

6. What is the purpose of the discriminator property?

In schemas using polymorphism (oneOf or anyOf), the discriminator helps identifying which sub-schema is being used based on a specific field value (like type or objectKind).


Advanced Level: Architecture & Security

7. How do you implement Security Schemes?

1. Define the scheme under components/securitySchemes (e.g., OAuth2, API Key).
2. Apply it using the security keyword either globally or at specific operation levels.

8. Explain the difference between oneOf, anyOf, and allOf.

  • allOf: Must satisfy all sub-schemas (inheritance).
  • anyOf: Must satisfy at least one sub-schema.
  • oneOf: Must satisfy exactly one sub-schema.

9. How do you handle file uploads in OpenAPI 3.x?

Set the request body content type to multipart/form-data and define the file property as type: string with format: binary.


Summary Comparison

Feature OpenAPI 2.0 (Swagger) OpenAPI 3.0+
Structure Single host/basePath Multiple servers supported
Content Types consumes / produces Defined per request/response
Components definitions components/schemas
«
»
Hibernate

Comments & Discussions