Skip to content

Commit 4352d7b

Browse files
committed
Merge branch 'main' into add-error-log-for-validator
2 parents b5dce09 + 56abad4 commit 4352d7b

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1-
# README
1+
# Documentation first API development
22

3-
This README would normally document whatever steps are necessary to get the
4-
application up and running.
3+
### What is Documentation-First Approach and its Benefits
54

6-
Things you may want to cover:
5+
The documentation-first approach, also known as design-first or API-first, emphasizes creating API documentation before writing any code.The documentation itself acts as a specification.
76

8-
* Ruby version
7+
### Benefits of Documentation-First Approach
98

10-
* System dependencies
9+
- **Clarity and Communication**: By documenting the API first, all team members, including developers, testers, and business stakeholders, have a clear understanding of the API's purpose and functionality.
10+
- **Early Feedback**: Documentation can be reviewed and validated by stakeholders before any code is written, allowing for early detection of potential issues or misunderstandings.
11+
- **Consistency**: Ensures that the implementation adheres strictly to the agreed-upon design, reducing the risk of discrepancies between the documentation and the actual API.
12+
- **Improved Collaboration**: Facilitates better collaboration between different teams (e.g., frontend and backend) as they can work from a shared, well-defined API specification.
13+
- **Better Testing**: Test cases can be derived from the documentation, ensuring comprehensive test coverage and validation against the specified API behavior.
14+
- **Ease of Maintenance**: Having a well-documented API makes it easier to maintain and update, as changes can be tracked and communicated effectively through the documentation.
1115

12-
* Configuration
16+
By adopting a documentation-first approach, teams can create more reliable, understandable, and maintainable APIs, ultimately leading to better software quality and user satisfaction.
1317

14-
* Database creation
18+
### OpenAPI Specification
1519

16-
* Database initialization
20+
The OpenAPI Specification (OAS) is a standard for defining and describing RESTful APIs. It allows both humans and computers to understand the capabilities of a service without accessing its source code or documentation.
1721

18-
* How to run the test suite
22+
OpenAPI uses a different specification: JSON Schema. This can be used to validate JSON even outside OpenAPI, and it’s a great tool; it’s really readable and easy to understand, but it’s a bit verbose. To fix that, we can use references:
1923

20-
* Services (job queues, cache servers, search engines, etc.)
24+
e.g., OpenAPI schema: [Petstore Example](https://petstore3.swagger.io/#/)
2125

22-
* Deployment instructions
26+
### Gem Used: `committee`
2327

24-
* ...
28+
We used the `committee` gem for our API documentation and validation. It helps in ensuring that our API conforms to the OpenAPI specification.
29+
30+
Altenative gem : [Skooma](https://github.com/skryukov/skooma), [Open-api-first](https://github.com/ahx/openapi_first)
31+
32+
### Non Breaking change
33+
34+
- Add headers to requests, only do validation if headers persent
35+
This is how the request validation added
36+
- For non-breaking changes, instead of returning errors, we log the errors. This approach helps in maintaining the stability of the API while still capturing any issues that need to be addressed.
37+
- this is how the response validation added
38+
39+
### Documenation UI
40+
41+
Scalar is add for open api schema UI

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Application < Rails::Application
3030
# Skip views, helpers and assets when generating a new resource.
3131
config.api_only = true
3232

33+
# Add the request and response validation middleware to the application.
34+
# TODO: This can be changed later to add middleware in exact position e.g config.middleware.after Rack::Runtime, OpenApiSchema::RequestValidatorMiddleware
3335
config.middleware.use ::OpenApiSchema::RequestValidatorMiddleware
3436
config.middleware.use ::OpenApiSchema::ResponseValidatorMiddleware
3537
end

lib/open_api_schema/request_validator_middleware.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ def initialize(app)
77
end
88

99
# Handles the middleware call to validate the schema if the "VALIDATE_SCHEMA" header is present.
10-
# If the header is set to "1", it validates the request schema.
11-
# Otherwise, it continues the request-response cycle as usual.
10+
# If the header VALIDATE_SCHEMA is exist, it validates the request schema.
1211
#
1312
# @param env [Hash] The Rack environment hash.
1413
# @return [Array] The status, headers, and response.

lib/open_api_schema/response_validator_middleware.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(app)
3939
@response_validator = Committee::Middleware::ResponseValidation.new(app, schema_path: "docs/openapi.json", ignore_error: true, error_handler: error_handler)
4040
end
4141

42-
# Handles the middleware call to validate the schema if the "VALIDATE_SCHEMA" header is present.
42+
# Sets up the middleware to validate the response schema if the "VALIDATE_SCHEMA" header is present.
4343
#
4444
# @param env [Hash] The Rack environment hash.
4545
# @return [Array] The status, headers, and response.
@@ -54,7 +54,6 @@ def call(env)
5454
end
5555

5656
private
57-
5857
def condition_for_response_validation?(env)
5958
true # env.key?("HTTP_VALIDATE_SCHEMA")
6059
end

0 commit comments

Comments
 (0)