Skip to content
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
62 changes: 31 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/oapi-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ workspace = true
anstream = "1.0.0"
clap = { version = "4.6.6", features = ["derive"] }
http = "1.5.0"
indexmap = "2.14.0"
indexmap = "2.14.1"
openapiv3 = "2.2.0"
owo-colors = "4.3.0"
owo-colors = "4.4.0"
prettyplease = "0.3.0"
proc-macro2 = "1.0.107"
quote = "1.0.47"
regex = { version = "1.13.1", default-features = false, features = ["std", "perf", "unicode"] }
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.151"
serde_yaml = "0.9.34"
syn = { version = "3.0.3", features = ["full"] }
syn = { version = "3.0.4", features = ["full"] }

[dev-dependencies]
axum = { version = "0.8.9", features = ["multipart"] }
Expand All @@ -53,4 +53,4 @@ reqwest = { version = "0.13.4", default-features = false, features = ["blocking"
serde = { version = "1.0.229", features = ["derive"] }
serde_urlencoded = "0.7.1"
trycmd = "1.2.1"
uuid = { version = "1.24.1", features = ["serde"] }
uuid = { version = "1.26.0", features = ["serde"] }
11 changes: 5 additions & 6 deletions crates/oapi-codegen/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod operation;
mod package;
mod reqwest;
mod servers;
mod usage;
pub(crate) mod usage;

use std::collections::HashMap;

Expand Down Expand Up @@ -300,13 +300,12 @@ fn render_body(items: &[TokenStream]) -> Result<String> {
}

/// Render a doc attribute, or nothing when there is no documentation.
///
/// This splits the text on its line breaks, so a multi-line `description` prints
/// as a run of `///` lines and not one `/** */` block.
pub(crate) fn doc_attr(doc: &Option<String>) -> TokenStream {
let tokens = match doc {
Some(text) => {
// Leading space matches the `/// text` desugaring rustfmt produces.
let spaced = format!(" {text}");
quote! { #[doc = #spaced] }
}
Some(text) => doc_lines(std::slice::from_ref(text)),
None => quote! {},
};
return tokens;
Expand Down
2 changes: 2 additions & 0 deletions crates/oapi-codegen/src/emit/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ fn emit_alias(alias: &Alias) -> Result<TokenStream> {
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::Access;
use crate::naming::Case;
use crate::naming::to_ident;

Expand All @@ -536,6 +537,7 @@ mod tests {
serde_skip: false,
default: Some(DefaultValue::Int(10)),
constraints: None,
access: Access::ReadWrite,
}],
additional_properties: None,
deny_unknown_fields: false,
Expand Down
38 changes: 21 additions & 17 deletions crates/oapi-codegen/src/emit/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::collections::HashMap;
use crate::emit::Targets;
use crate::emit::models::ModelDerives;
use crate::emit::models::SerdeDerives;
use crate::ir::Direction;
use crate::ir::ForeignDerives;
use crate::ir::Item;
use crate::ir::Module;
Expand All @@ -32,11 +33,11 @@ use crate::naming::to_ident;

/// Whether a model is reachable as a request payload and/or a response payload.
#[derive(Debug, Default, Clone, Copy)]
struct Usage {
pub(crate) struct Usage {
/// Reachable from a request body or request-input struct.
request: bool,
pub(crate) request: bool,
/// Reachable from a response body.
response: bool,
pub(crate) response: bool,
}

/// Compute the derive set for every generated model, keyed by its logical name.
Expand All @@ -46,14 +47,7 @@ struct Usage {
pub(crate) fn model_derives(module: &Module, service: &Service, targets: Targets) -> HashMap<String, ModelDerives> {
let adjacency = adjacency(module);
let foreign = foreign_derives(module, &adjacency);
let mut usage: HashMap<String, Usage> = HashMap::new();

for name in request_seeds(service) {
mark(&adjacency, &name, &mut usage, Direction::Request);
}
for name in response_seeds(service) {
mark(&adjacency, &name, &mut usage, Direction::Response);
}
let usage = direction_usage(module, service);

// Union of both keys. A model can be constrained by a foreign type without
// being reachable from any operation, and the other way round, so taking only
Expand Down Expand Up @@ -264,11 +258,19 @@ fn item_types(item: &Item) -> Vec<RustType> {
return types;
}

/// The direction a seed propagates.
#[derive(Debug, Clone, Copy)]
enum Direction {
Request,
Response,
/// Which direction reaches each model, keyed by its logical name.
///
/// An absent name is reached by no operation, which happens under `skip-prune`.
pub(crate) fn direction_usage(module: &Module, service: &Service) -> HashMap<String, Usage> {
let adjacency = adjacency(module);
let mut usage: HashMap<String, Usage> = HashMap::new();
for name in request_seeds(service) {
mark(&adjacency, &name, &mut usage, Direction::Request);
}
for name in response_seeds(service) {
mark(&adjacency, &name, &mut usage, Direction::Response);
}
return usage;
}

/// Mark `start` and every model reachable from it with `direction`, following
Expand Down Expand Up @@ -301,7 +303,7 @@ fn mark(

/// Build the model-reference graph: each item name mapped to the names of the
/// generated models it references through its fields, variants, or alias target.
fn adjacency(module: &Module) -> HashMap<String, Vec<String>> {
pub(crate) fn adjacency(module: &Module) -> HashMap<String, Vec<String>> {
let mut graph = HashMap::with_capacity(module.items.len());
for item in &module.items {
graph.insert(item.name().to_owned(), item_references(item));
Expand Down Expand Up @@ -427,6 +429,7 @@ fn struct_field_names(strukt: &Struct, out: &mut Vec<String>) {
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::Access;
use crate::ir::Alias;
use crate::ir::Body;
use crate::ir::Field;
Expand Down Expand Up @@ -457,6 +460,7 @@ mod tests {
serde_skip: false,
default: None,
constraints: None,
access: Access::ReadWrite,
};
}

Expand Down
Loading
Loading