GREsau/okapi

How to use JsonSchema ???

DilecPadovani opened this issue · 8 comments

I'm trying to generate schemas from structs to use inside rocket_okapi. Following the documentation I'm doing :

use rocket_okapi::JsonSchema;

use crate::schema::counters;

#[derive( Debug, JsonSchema)]
pub struct Counter {
    pub id: i32,
    pub name: String,
    pub counter: i32,
}

but I receive this error about JsonSchema :

error[E0576]: cannot find method or associated constant `add_schema_as_property` in trait `schemars::JsonSchema`
 --> src/database/models.rs:7:13
  |
7 |     pub id: i32,
  |             ^^^ not found in `schemars::JsonSchema`

error[E0576]: cannot find method or associated constant `add_schema_as_property` in trait `schemars::JsonSchema`
 --> src/database/models.rs:8:15
  |
8 |     pub name: String,
  |               ^^^^^^ not found in `schemars::JsonSchema`

error[E0576]: cannot find method or associated constant `add_schema_as_property` in trait `schemars::JsonSchema`
 --> src/database/models.rs:9:18
  |
9 |     pub counter: i32,
  |                  ^^^ not found in `schemars::JsonSchema`

error[E0599]: no method named `apply_metadata` found for mutable reference `&mut SchemaGenerator` in the current scope
 --> src/database/models.rs:5:28
  |
5 | #[derive(Queryable, Debug, JsonSchema)]
  |                            ^^^^^^^^^^ method not found in `&mut SchemaGenerator`
  |
  = note: this error originates in the derive macro `JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)

my cargo.toml is :

okapi =  { version = "0.4", features = ["derive_json_schema"] }
rocket_okapi = { version = "0.5.1", feature = ["impl_json_schema"] }
schemars = { version = "0.8.10" }
serde = "1.0.136"

I would like to know what I'm doing wrong, I tried adding many feature flags in the Cargo.toml, but nothing seems to be working. Looking forward to hearing from you.

I think this is a version conflict.
rocket_okapi v0.5.1 depends on schemars v0.7.0, not 0.8.10. And the same trait from 2 different versions are not treated the same.
I recommend using rocket_okapi version 0.8.0-rc.2.
This way rocket_okapi uses version 0.8.10 of schemars.

Thank you, i think i was using an inappropriate crate of rocket_contrib which from my understanding is going to be deprecated.
If I may there is a lack of examples, I would like to contribute giving the work I did at moment: my repo.
My work consist on creating a rocket API endpoint, which can be described by a swagger documentation created using rocket_okapi and also in the mix there is the connection to a postgres DB using diesel.
I would like to know if what I did is the right implementation of the new version, btw fell free to make an example out of it.

For Rocket is recommend switching to the RC (Release candidate). It is very stable, I have used it quite a bit. (I see you just did that in the last commit)

If I may there is a lack of examples
Really? There are quite a few here:
https://github.com/GREsau/okapi/tree/master/examples
Maybe they are not that visible?

I recommend checking out this on: https://github.com/GREsau/okapi/tree/master/examples/custom_schema
It shows how to structure a project that might grow quickly in the future.

I also recommend checking out RapiDocs, I think it looks better then SwaggerUI. And you can have both too.
(it is included in rocket_okapi too)

If you are looking for more inspiration you can also look here: https://gitlab.com/df_storyteller/df-storyteller
(it uses a bit older versions, so things have changed a bit since then)

I also have some crates I made open source from work. I don't recommend depending on them, but free free to fork or copy the code. https://github.com/MyEmma1/myemma-helper-crates
These mainly have to do with logging and error handling.

For database things also look at the FAQ: https://github.com/GREsau/okapi#faq
There is one Q about Databases.

If you have more questions let me know.

I would like to know if what I did is the right implementation of the new version, btw fell free to make an example out of it.

  • Make sure you have a README.md file in your repo.
  • The token file might not be needed.
  • Don't include .env create a .env.example and add the other file to the .gitignore. (this is because it might lead to passwords being included in a repo, so should be avoided)
  • the .vscode folder is already in repo, but also in .gitignore.
  • In the dev.api.Dockerfile file I think the following lines are not needed. (looks like you are copying the code into the final build container)

https://github.com/DILECPEDO/rocket_diesel_demo/blob/e6b0f18db000055499be5e7d975c5e7de7fa280e/dev.api.Dockerfile#L3:L8

  • The latest Rocket release works on stable so #![feature(proc_macro_hygiene, decl_macro)] and nightly are not needed.
  • Don't forget to add a license otherwise people can not use it.
  • ...

That is a good start :)

As you said everything should work on stable but when I run this command inside docker I get this error:

#0 188.4   --- stderr
#0 188.4   Error: Pear requires a 'dev' or 'nightly' version of rustc.
#0 188.4   Installed version: 1.61.0 (2022-05-18)
#0 188.4   Minimum required:  1.31.0-nightly (2018-10-05)

This is my docker file:

FROM rust:slim as builder
WORKDIR /usr/src/api_app
COPY Rocket.toml .
COPY Cargo.toml .
COPY diesel.toml .
COPY src src
COPY migrations migrations
RUN cargo install --path .

FROM debian:buster-slim
# RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/api_app /usr/local/bin/api_app
ENTRYPOINT ["api_app"]   

The package rocket_contrib was moved into rocket in version 0.5.0-rc.1 and later.

Graduation of json, msgpack, and uuid rocket_contrib features into core.

Source: https://github.com/SergioBenitez/Rocket/blob/v0.5-rc/CHANGELOG.md#major-features-and-improvements-1

The rocket_contrib crate has been deprecated and should no longer be used.

Source: https://github.com/SergioBenitez/Rocket/blob/v0.5-rc/CHANGELOG.md#contrib-graduation

The package uses an older version of pear which still needed nightly.

Just remove the rocket_contrib dependency.

More info: https://rocket.rs/v0.5-rc/guide/upgrading/
https://rocket.rs/v0.5-rc/guide/upgrading/#contrib-deprecation

About move to stable: https://rocket.rs/v0.5-rc/guide/upgrading/#stable-and-async-support

Also link above to project does not work anymore (branch was removed).
So posting new link https://github.com/DILECPEDO/rocket_diesel_demo (for myself and future people that end up here)

ralpha commented

Closing issue as I don't think this is relevant anymore and was fixed.
Rocket v0.5 is now stable.