Skip to content

Commit

Permalink
Merge pull request #511 from that-ambuj/add-openapi-toschema-derive
Browse files Browse the repository at this point in the history
Derive utoipa::ToSchema by default for generating openapi specs by other libraries
  • Loading branch information
Byron authored Jun 22, 2024
2 parents 7b70b66 + 4942a84 commit 501f733
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generator/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ def unique(
original: str,
desired: str,
attempts: int = 0,
assigned: dict[str, str] = {},
assigned: Dict[str, str] = {},
) -> str:
if original in assigned:
return assigned[original]
Expand Down
2 changes: 2 additions & 0 deletions src/generator/templates/Cargo.toml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ hyper-rustls = "0.25.0"
## Must match the one hyper uses, otherwise there are duplicate similarly named `Mime` structs
mime = "^ 0.3.0"
serde = { version = "^ 1.0", features = ["derive"] }
utoipa = { version = "^4.2", optional = true }
serde_json = "^ 1.0"
itertools = "0.13"
% if cargo.get('is_executable', False):
Expand Down Expand Up @@ -61,4 +62,5 @@ version = "${util.crate_version()}"
[features]
yup-oauth2 = ["google-apis-common/yup-oauth2"]
default = ["yup-oauth2"]
utoipa = ["dep:utoipa"]
% endif
8 changes: 8 additions & 0 deletions src/generator/templates/api/lib/lib.mako
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ Arguments will always be copied or cloned into the builder, to make them indepen
[wiki-pod]: http://en.wikipedia.org/wiki/Plain_old_data_structure
[builder-pattern]: http://en.wikipedia.org/wiki/Builder_pattern
[google-go-api]: https://github.com/google/google-api-go-client
${'##'} Cargo Features
* `utoipa` - Add support for [utoipa](https://crates.io/crates/utoipa) and derive `utoipa::ToSchema` on all
the types. You'll have to import and register the required types in `#[openapi(schemas(...))]`, otherwise the
generated `openapi` spec would be invalid.
</%def>

## Sets up a hub ready for use. You must wrap it into a test function for it to work
Expand Down Expand Up @@ -308,6 +315,7 @@ ${self.test_hub(hub_type(c.schemas, util.canonical_name()))}
% endif
</%def>


###############################################################################################
###############################################################################################
<%def name="license()">\
Expand Down
1 change: 1 addition & 0 deletions src/generator/templates/api/lib/schema.mako
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ ${struct} { _never_set: Option<bool> }
<%block filter="rust_doc_sanitize(documentationLink), rust_doc_comment">\
${doc(s, c)}\
</%block>
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde_with::serde_as(crate = "::client::serde_with")]
#[derive(${', '.join(traits)})]
% if s.type == 'object':
Expand Down

0 comments on commit 501f733

Please sign in to comment.