Skip to content

Conversation

@markhilb
Copy link
Contributor

Don't see why this is a problem.
Correct me if i'm wrong.

@kurtbuilds
Copy link
Owner

It's been a while since I looked at this, but I believe the issue is the Baz schema that the reference refers to is never added to the spec.

Maybe that's ok? Or it prints a warning? But silently creating an invalid spec feels wrong.

@markhilb
Copy link
Contributor Author

I'm not sure which spec you mean.
If you mean Foo::schema, then Baz is not added regardless of whether it is flattened or not.
If you mean the openapi spec of a Server then it is added.

use actix_web::web::Json;
use oasgen::{OaSchema, Server, oasgen};
use serde::Serialize;

#[derive(OaSchema, Serialize)]
pub struct Baz {
    value: i32,
}

#[derive(OaSchema, Serialize)]
pub struct Bar {
    baz: Baz,
}

#[derive(OaSchema, Serialize)]
pub struct Foo {
    #[serde(flatten)]
    flattened: Bar,
}

#[oasgen]
async fn foo() -> Json<Foo> {
    todo!()
}

fn main() {
    let server = Server::actix().get("/foo", foo);

    let schema = serde_yaml::to_string(&Foo::schema()).unwrap();
    let spec = serde_yaml::to_string(&server.openapi).unwrap();

    println!("{schema}");
    println!();
    println!("{spec}");
}
type: object
properties:
  baz:
    $ref: '#/components/schemas/Baz'
required:
- baz


openapi: 3.0.3
info:
  title: ''
  version: ''
paths:
  /foo:
    get:
      operationId: foo
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'
components:
  schemas:
    Bar:
      type: object
      properties:
        baz:
          $ref: '#/components/schemas/Baz'
      required:
      - baz
    Baz:
      type: object
      properties:
        value:
          type: integer
      required:
      - value
    Foo:
      type: object
      properties:
        baz:
          $ref: '#/components/schemas/Baz'
      required:
      - baz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants