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

[#124] query_parameters 자동 문서화 기능 구현 #128

Merged
merged 6 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions rupring/src/example/domains/users/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub struct CreateUserRequest {
pub _asdf: Option<foo::Bar>,
pub ids: Vec<i32>,
pub bars: Vec<foo::Bar>,

#[query = "query_test111"]
#[desc = "설명"]
#[example = "asdf"]
pub query_test: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions rupring/src/swagger/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ fn generate_swagger(swagger: &mut SwaggerSchema, root_module: Box<dyn crate::IMo
for swagger_parameter in swagger_request_body.path_parameters {
operation.parameters.push(swagger_parameter);
}

for swagger_parameter in swagger_request_body.query_parameters {
operation.parameters.push(swagger_parameter);
}
}

// TODO: 추후에는 swagger ignore 속성을 추가해서 그걸로 처리
Expand Down
141 changes: 94 additions & 47 deletions rupring_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
let mut is_ignore = false;

let mut is_path_parameter = false;
let mut is_query_parameter = false;

if field_type.starts_with("Option<") {
is_required = false;
Expand All @@ -604,60 +605,87 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
for attribute in attributes {
let metadata = attribute.meta;

if let Ok(meta_name_value) = metadata.require_name_value() {
if let Some(segement) = meta_name_value.path.segments.get(0) {
let attribute_key = segement.ident.to_string();
let path = metadata.path().to_token_stream().to_string();

match attribute_key.to_lowercase().as_str() {
"example" => {
if let Expr::Lit(lit) = &meta_name_value.value {
example = format!("{:?}", lit.to_token_stream().to_string());
}
}
"required" => {
if let Expr::Lit(lit) = &meta_name_value.value {
is_required = lit.to_token_stream().to_string().parse().unwrap();
} else {
is_required = true;
}
}
"description" | "desc" => {
if let Expr::Lit(lit) = &meta_name_value.value {
let mut text = lit.to_token_stream().to_string();

if text.starts_with("\"") {
text = text
.trim_start_matches("\"")
.trim_end_matches("\"")
.to_string();
}

description = text;
}
let meta_value = metadata.require_name_value().ok().map(|e| e.value.clone());

match path.to_lowercase().as_str() {
"example" => {
if let Some(Expr::Lit(lit)) = &meta_value {
example = format!("{:?}", lit.to_token_stream().to_string());
}
}
"required" => {
if let Some(Expr::Lit(lit)) = &meta_value {
is_required = lit.to_token_stream().to_string().parse().unwrap();
} else {
is_required = true;
}
}
"description" | "desc" => {
if let Some(Expr::Lit(lit)) = &meta_value {
let mut text = lit.to_token_stream().to_string();

if text.starts_with("\"") {
text = text
.trim_start_matches("\"")
.trim_end_matches("\"")
.to_string();
}
"name" => {
if let Expr::Lit(lit) = &meta_name_value.value {
let mut text = lit.to_token_stream().to_string();

if text.starts_with("\"") {
text = text
.trim_start_matches("\"")
.trim_end_matches("\"")
.to_string();
}

field_name = text;
}

description = text;
}
}
"name" => {
if let Some(Expr::Lit(lit)) = &meta_value {
let mut text = lit.to_token_stream().to_string();

if text.starts_with("\"") {
text = text
.trim_start_matches("\"")
.trim_end_matches("\"")
.to_string();
}
"path_param" | "param" => {
is_path_parameter = true;

field_name = text;
}
}
"path_param" | "param" => {
if let Some(Expr::Lit(lit)) = &meta_value {
let mut text = lit.to_token_stream().to_string();

if text.starts_with("\"") {
text = text
.trim_start_matches("\"")
.trim_end_matches("\"")
.to_string();
}
"ignore" => {
is_ignore = true;

field_name = text;
}

is_path_parameter = true;
}
"query" => {
is_query_parameter = true;

if let Some(Expr::Lit(lit)) = &meta_value {
let mut text = lit.to_token_stream().to_string();

if text.starts_with("\"") {
text = text
.trim_start_matches("\"")
.trim_end_matches("\"")
.to_string();
}
_ => {}

field_name = text;
}
}
"ignore" => {
is_ignore = true;
}
_ => {}
}
}

Expand Down Expand Up @@ -694,6 +722,25 @@ pub fn derive_rupring_doc(item: TokenStream) -> TokenStream {
continue;
}

if is_query_parameter {
code += format!(
r#"swagger_definition.query_parameters.push(rupring::swagger::json::SwaggerParameter {{
name: "{field_name}".to_string(),
in_: rupring::swagger::json::SwaggerParameterCategory::Query,
description: "{description}".to_string(),
required: {is_required},
schema: Some(rupring::swagger::json::SwaggerTypeOrReference::Type(
rupring::swagger::json::SwaggerType {{
type_: "{field_type}".to_string(),
}}
)),
type_: None,
}});"#
).as_str();

continue;
}

// Body 파라미터 생성 구현
code += format!(r#"let property_of_type = {field_type}::to_swagger_definition(context);"#)
.as_str();
Expand Down
Loading