-
Notifications
You must be signed in to change notification settings - Fork 188
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
Missing references for ToSchema when types are defined in dependencies #894
Comments
I have found a hard workaround for this. I have to go through each of the fields of the dependencies and list them manually under components.
Another issue I had is that the schema prints So I had to write a modifier to change the opanapi.json responses /// Remove the models. prefix from the openapi json
fn modify_openapi_json(openapi_json: &str) -> String {
return openapi_json.replace("#/components/schemas/models.", "#/components/schemas/");
} To print the fixed schema: // in the main function
let openapi = ApiDoc::openapi();
println!("{}", modify_openapi_json(&openapi.to_json()?)); To modify the openapi response for Actix App::new()
.service(scope("/api/v1/openapi3.json").wrap_fn(|request, service| {
let is_open_api = request.path() == "/api/v1/openapi3.json";
return service.call(request).map(move |result| {
if !is_open_api {
return result;
}
// Modify the response to remove the models. prefix from the openapi json
return result.map(|result| {
return result.map_body(|_header, body| {
// Extract the openapi json from the body
let mut bytes = body
.try_into_bytes()
.expect("Failed to convert body to bytes");
let body_str =
str::from_utf8(&mut bytes).expect("Failed to convert bytes to string");
let modified_body_str = modify_openapi_json(body_str);
return BoxBody::new(modified_body_str);
});
});
});
}))
.service(
RapiDoc::with_openapi("/api/v1/openapi3.json", openapi.clone()).path("/api/v1/docs/"),
); |
I'm pretty sure that's the "intended" way. Utoipa doesn't include any types that don't derive ToSchema, see https://github.com/juhaku/utoipa/blob/master/utoipa/src/lib.rs#L528 for how the "standard" types are derived. FWIW, I do the same in my project, but I didn't have to modify any paths |
As @junlarsen stated it does work as intended. However there are two separate things going on here with your particular question.
This is not a workaround, that is as it is intended. Utoipa requires explicit declaration of types that suppose to be present in the OpenAPI spec. You might be interested of this crate https://github.com/ProbablyClem/utoipauto. It will enable automatic schema and path recognition. But still the types to be recognized must implement
From the docs https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html#struct-optional-configuration-options-for-schema:
That is the schema path declaration used in #[openapi(
schema(models::MyType)
)] |
@juhaku Regarding Point 2. Is it possible to rename the referenced Schama name for id:
$ref: '#/components/schemas/crate.reference_id.ReferenceId' to id:
$ref: '#/components/schemas/ReferenceId' |
@bennobuilder Currently you could use something like this: struct MyType {
#[schema(value_type = ReferenceId)]
value: crate::reference_id::ReferenceId
} Above code will override the type schema and name for the generated schema. |
I have added for
ToSchema
for aMyType
defined in a Cargo dependency. However, when I declareMyType
as a components of the schema in my project, the fields of the third party type, have missing models.Here you can see that
MyTypeAttributes
,MyTypeRelationships
, andType
are not generated in the openapi schema.It seems that the macro only goes deep one level, and doesn't consider that the fields of a struct can have schemas themselves.
The text was updated successfully, but these errors were encountered: