Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skeleton for openapi implementation #43

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions atelier-openapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::cell::RefCell;
use std::io::Write;
use std::str::FromStr;

use atelier_core::error::{Error as ModelError, Result as ModelResult};
use atelier_core::error::Error as ModelError;
use atelier_core::model::shapes::{AppliedTraits, Service};
use atelier_core::model::values::Value;
use atelier_core::model::visitor::walk_model;
use atelier_core::model::{Identifier, Model, ShapeID};
use atelier_core::model::{Model, ShapeID};
use atelier_core::{io::ModelWriter, model::visitor::ModelVisitor};
use okapi::openapi3;
use serde_json::to_writer_pretty;
Expand Down Expand Up @@ -53,6 +53,18 @@ impl ModelWriter for OpenApiWriter {
}
}

impl OpenApiModelVisitor {
fn add_info_object(&self, title: String, version: String) {
let mut spec = self.spec.borrow_mut();

spec.info = openapi3::Info {
version,
title,
..openapi3::Info::default()
};
}
}

impl ModelVisitor for OpenApiModelVisitor {
type Error = ModelError;

Expand All @@ -62,21 +74,18 @@ impl ModelVisitor for OpenApiModelVisitor {
traits: &AppliedTraits,
shape: &Service,
) -> Result<(), Self::Error> {
let mut spec = self.spec.borrow_mut();

let title_trait_id = &ShapeID::from_str("smithy.api#title").unwrap();

let title = expect_string_trait_value(traits, title_trait_id)
.unwrap_or(format!("{}", id.shape_name()));

// let mut info = openapi3::Info::default();
// info.version = shape.version().clone();
spec.info = openapi3::Info {
version: shape.version().clone(),
title,
..openapi3::Info::default()
let title = if traits.contains_key(title_trait_id) {
expect_string_trait_value(traits, title_trait_id)
} else {
Comment on lines +79 to +81
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is the best way to go about extracting traits. I'd like to is_titled but HasTraits is not implemented for Service. Not sure how this all fits together just yet

format!("{}", id.shape_name())
};

let version = shape.version().clone();

self.add_info_object(title, version);

Ok(())
}
}
Expand All @@ -85,16 +94,14 @@ impl ModelVisitor for OpenApiModelVisitor {
// Private Functions
// ------------------------------------------------------------------------------------------------

// TODO: tighten up types, should error if trait doesn't have string value
// TODO: also needs better name
fn expect_string_trait_value(traits: &AppliedTraits, trait_id: &ShapeID) -> Option<String> {
fn expect_string_trait_value(traits: &AppliedTraits, trait_id: &ShapeID) -> String {
if let Some(Some(trait_value)) = traits.get(trait_id) {
match trait_value {
Value::String(s) => Some(s.clone()),
_ => None,
Value::String(s) => s.clone(),
v => panic!("Expected trait {} to be a string but was: {}", trait_id, v),
}
} else {
None
panic!("Expected trait {} not found", trait_id)
}
}

Expand Down
2 changes: 1 addition & 1 deletion atelier-openapi/tests/describe_openapi_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn test(file_name: &str) {
MANIFEST_DIR, file_name
));
let expected_file_path = PathBuf::from(format!(
"{}/tests/models/{}.openapi.jsoN",
"{}/tests/models/{}.openapi.json",
MANIFEST_DIR, file_name
));

Expand Down