Skip to content

Commit

Permalink
Deneme (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zelosleone authored Dec 25, 2024
1 parent 586b7b1 commit aea1b7b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 51 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/openapi-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: OpenAPI Tests
on:
push:
branches:
- main
pull_request:
paths:
- 'golem-worker-service-base/tests/openapi_swagger_tests.rs'
- '**/openapi/**'
- '**/swagger/**'

jobs:
openapi-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: recursive

- name: Fetch tag
run: git fetch origin --deepen=1

- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt, clippy

- uses: Swatinem/rust-cache@v2
with:
shared-key: openapi-tests
cache-all-crates: true

- name: Install Protoc
uses: arduino/setup-protoc@v3
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: davidB/rust-cargo-make@v1

- name: Build all targets
run: cargo make --profile ci build

- name: Build tests
run: cargo test --package golem-worker-service-base --test openapi_swagger_tests --no-run

- name: Create output directory
run: mkdir -p openapi_exports

- name: Run OpenAPI tests
run: |
RUST_LOG=info cargo test --package golem-worker-service-base --test openapi_swagger_tests -- --nocapture
env:
RUST_BACKTRACE: 1
CARGO_TERM_COLOR: always

- name: Upload OpenAPI artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: openapi-exports
path: openapi_exports/

- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: success() || failure()
with:
report_paths: '**/target/report-*.xml'
detailed_summary: true
include_passed: true

permissions:
contents: read
checks: write
pull-requests: write
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub use swagger_ui::{
mod http_api_definition;
mod http_api_definition_request;
mod http_oas_api_definition;
mod openapi_export;
mod openapi_converter;
mod rib_converter;
mod swagger_ui;
pub mod openapi_export;
pub mod openapi_converter;
pub mod rib_converter;
pub mod swagger_ui;
pub(crate) mod path_pattern_parser;
pub(crate) mod place_holder_parser;
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use utoipa::openapi::OpenApi;
use std::sync::Arc;
use utoipa::openapi::OpenApi;
use crate::gateway_api_definition::http::openapi_export::OpenApiExporter;

pub struct OpenApiConverter {
Expand Down Expand Up @@ -80,36 +80,45 @@ impl Default for OpenApiConverter {

#[cfg(test)]
mod tests {
use super::OpenApiConverter;
use utoipa::openapi::{
OpenApi,
Server,
Tag,
};

#[test]
fn test_openapi_converter() {
let _converter = super::OpenApiConverter::new();
let _converter = OpenApiConverter::new();

// Create base OpenAPI
let mut base = utoipa::openapi::OpenApi::new();
base.paths.paths.insert("/base".to_string(), utoipa::openapi::PathItem::new()
.get(utoipa::openapi::path::Operation::new().description("Base operation")));
base.components = Some(utoipa::openapi::Components::new()
.schema("BaseSchema", utoipa::openapi::Schema::Object(utoipa::openapi::schema::Object::new()))
.response("BaseResponse", utoipa::openapi::Response::new("Base response"))
.security_scheme("BaseAuth", utoipa::openapi::security::SecurityScheme::ApiKey(utoipa::openapi::security::ApiKey::Header("X-Base-Auth".to_string()))));
base.security = Some(vec![utoipa::openapi::SecurityRequirement::new("BaseAuth")]);
base.tags = Some(vec![utoipa::openapi::Tag::new("base")]);
base.servers = Some(vec![utoipa::openapi::Server::new("/base")]);
let mut base = OpenApi::new(Default::default(), ());
let get_op = OperationBuilder::new().summary(Some("Base operation".to_string())).build();
let mut path_item = PathItem::new(HttpMethod::Get, ());
path_item.get = Some(get_op);
base.paths.paths.insert("/base".to_string(), path_item);
let mut components = Components::new();
components.schemas.insert("BaseSchema".to_string(), Schema::Object(Object::new()).into());
base.components = Some(components);
base.security = Some(vec![SecurityRequirement::new("BaseAuth", ())]);
base.tags = Some(vec![Tag::new("base")]);
base.servers = Some(vec![Server::new("/base")]);

// Create other OpenAPI with duplicate path
let mut other = utoipa::openapi::OpenApi::new();
other.paths.paths.insert("/base".to_string(), utoipa::openapi::PathItem::new()
.post(utoipa::openapi::path::Operation::new().description("Other operation")));
other.components = Some(utoipa::openapi::Components::new()
.schema("OtherSchema", utoipa::openapi::Schema::Object(utoipa::openapi::schema::Object::new()))
.response("OtherResponse", utoipa::openapi::Response::new("Other response"))
.security_scheme("OtherAuth", utoipa::openapi::security::SecurityScheme::ApiKey(utoipa::openapi::security::ApiKey::Header("X-Other-Auth".to_string()))));
other.security = Some(vec![utoipa::openapi::SecurityRequirement::new("OtherAuth")]);
other.tags = Some(vec![utoipa::openapi::Tag::new("other")]);
other.servers = Some(vec![utoipa::openapi::Server::new("/other")]);
let mut other = OpenApi::new(Default::default(), ());
let post_op = OperationBuilder::new().summary(Some("Other operation".to_string())).build();
let mut path_item = PathItem::new(HttpMethod::Get, ());
path_item.post = Some(post_op);
other.paths.paths.insert("/base".to_string(), path_item);
let mut components = Components::new();
components.schemas.insert("OtherSchema".to_string(), Schema::Object(Object::new()).into());
other.components = Some(components);
other.security = Some(vec![SecurityRequirement::new("OtherAuth", ())]);
other.tags = Some(vec![Tag::new("other")]);
other.servers = Some(vec![Server::new("/other")]);

// Test merging with duplicates
let merged = super::OpenApiConverter::merge_openapi(base.clone(), other.clone());
let merged = OpenApiConverter::merge_openapi(base.clone(), other.clone());

// Verify paths merged and duplicates handled
assert!(merged.paths.paths.contains_key("/base"));
Expand All @@ -121,44 +130,41 @@ mod tests {
let components = merged.components.unwrap();
assert!(components.schemas.contains_key("BaseSchema"));
assert!(components.schemas.contains_key("OtherSchema"));
assert!(components.responses.contains_key("BaseResponse"));
assert!(components.responses.contains_key("OtherResponse"));
assert!(components.security_schemes.contains_key("BaseAuth"));
assert!(components.security_schemes.contains_key("OtherAuth"));

// Test empty component merging
let mut empty_base = utoipa::openapi::OpenApi::new();
let mut empty_base = OpenApi::new(Default::default(), ());
empty_base.components = None;
let merged = super::OpenApiConverter::merge_openapi(empty_base, other);
let merged = OpenApiConverter::merge_openapi(empty_base, other);
assert!(merged.components.is_some());
let components = merged.components.unwrap();
assert!(components.schemas.contains_key("OtherSchema"));
}

#[test]
fn test_openapi_converter_new() {
let converter = super::OpenApiConverter::new();
assert!(Arc::strong_count(&converter.exporter) == 1);
let converter = OpenApiConverter::new();
assert_eq!(Arc::strong_count(&converter.exporter), 1);
}

#[test]
fn test_merge_openapi_with_empty_fields() {
// Test merging when base has empty optional fields
let mut base = utoipa::openapi::OpenApi::new();
let mut base = OpenApi::new(Default::default(), ());
base.security = None;
base.tags = None;
base.servers = None;
base.components = None;

// Create other OpenAPI with all fields populated
let mut other = utoipa::openapi::OpenApi::new();
other.security = Some(vec![utoipa::openapi::SecurityRequirement::new("OtherAuth")]);
other.tags = Some(vec![utoipa::openapi::Tag::new("other")]);
other.servers = Some(vec![utoipa::openapi::Server::new("/other")]);
other.components = Some(utoipa::openapi::Components::new()
.schema("OtherSchema", utoipa::openapi::Schema::Object(utoipa::openapi::schema::Object::new())));

let merged = super::OpenApiConverter::merge_openapi(base, other.clone());
let mut other = OpenApi::new(Default::default(), ());
other.security = Some(vec![SecurityRequirement::new("OtherAuth", ())]);
other.tags = Some(vec![Tag::new("other")]);
other.servers = Some(vec![Server::new("/other")]);
let mut components = Components::new();
components.schemas.insert("OtherSchema".to_string(), Schema::Object(Object::new()).into());
other.components = Some(components);

let merged = OpenApiConverter::merge_openapi(base, other.clone());

// Verify all fields were properly merged
assert_eq!(merged.security, other.security);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ mod tests {
#[test]
fn test_openapi_export() {
let exporter = super::OpenApiExporter;
let mut openapi = utoipa::openapi::OpenApi::new();
let mut openapi = utoipa::openapi::OpenApi::new(Default::default(), ());

// Test JSON export
let json_format = OpenApiFormat { json: true };
let json_format = crate::gateway_api_definition::http::OpenApiFormat { json: true };
let exported_json = exporter.export_openapi(
"test-api",
"1.0.0",
Expand All @@ -72,7 +72,7 @@ mod tests {
assert!(exported_json.contains("1.0.0"));

// Test YAML export
let yaml_format = OpenApiFormat { json: false };
let yaml_format = crate::gateway_api_definition::http::OpenApiFormat { json: false };
let exported_yaml = exporter.export_openapi(
"test-api",
"1.0.0",
Expand All @@ -84,7 +84,7 @@ mod tests {
assert!(exported_yaml.contains("1.0.0"));

// Test invalid OpenAPI handling
let invalid_openapi = utoipa::openapi::OpenApi::new();
let invalid_openapi = utoipa::openapi::OpenApi::new(Default::default(), ());
let result = exporter.export_openapi(
"test-api",
"1.0.0",
Expand All @@ -94,7 +94,7 @@ mod tests {
assert!(!result.is_empty()); // Should return default value instead of failing

// Test YAML export with invalid OpenAPI
let yaml_format = OpenApiFormat { json: false };
let yaml_format = crate::gateway_api_definition::http::OpenApiFormat { json: false };
let result = exporter.export_openapi(
"test-api",
"1.0.0",
Expand All @@ -106,7 +106,7 @@ mod tests {

#[test]
fn test_openapi_format_default() {
let format = OpenApiFormat::default();
let format = crate::gateway_api_definition::http::OpenApiFormat::default();
assert!(format.json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,4 @@ mod tests {
_ => panic!("Expected object schema"),
}
}
}
}

0 comments on commit aea1b7b

Please sign in to comment.