Skip to content
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

schema response per content type #621

Open
eladchen opened this issue Feb 5, 2022 · 5 comments
Open

schema response per content type #621

eladchen opened this issue Feb 5, 2022 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@eladchen
Copy link

eladchen commented Feb 5, 2022

💬 Question here

Would you consider updating the schema response config to also support a response per the request "accept" header?

const schema = {
   response: {
      // Augment the current mapping (status code -> response schema) to support
      // status code -> accept header -> response schema
      200: {
         "application/vnd.bla.v1+json": ...
         "*/*": ...
      }
   }
}

fastify.get("/apples/:id", { schema } , (request, reply) => {
  switch (request.header["accept"]) {
    case "application/json":
       reply.send(...);
       break;
   
    case "application/vnd.bla.v1+json":
       reply.send(...);
       break;
  }
});

This would also be beneficial when fastify-swagger dynamically generates an OAS v3 definition.

fastify/fastify-swagger#117

@eladchen eladchen added the help wanted Extra attention is needed label Feb 5, 2022
@mcollina
Copy link
Member

mcollina commented Feb 5, 2022

I think you can define separate routes using the constraint option: https://www.fastify.io/docs/latest/Reference/Routes/#constraints. However you are a bit on uncharted territories as I do not think anybody as tested this pattern. Officially supporting it would be great in both fastify and fastify-swagger.

Would you like to send a few PRs?

@eladchen
Copy link
Author

eladchen commented Feb 5, 2022

Cool.

I'll try and find time to add this. Care to point me where in fastify source I should probably start?

@Eomm
Copy link
Member

Eomm commented Feb 13, 2022

@TimonVS
Copy link

TimonVS commented Jun 1, 2022

I'm running into the same issue, especially the OpenAPI/Swagger part. I'm willing to contribute a solution for this, but constraints are dynamic, so I struggle to see how we'd be able to infer the correct response or request body for the OpenAPI specification. Any guidance would be very helpful!

import headerConstraintStrategy from "header-constraint-strategy";

const api = fastify({
    // ...
    constraints: {
        accept: headerConstraintStrategy("accept");
    },
});

api.post(
    `/assets`,
    {
        schema: Type.Object({ file: Type.String({ format: "binary" }) }),
        // While the constraint doesn't look dynamic, it only works because we registered the constraint in the `fastify` constructor. Using this to infer the accept header could work if we standardize the `accept` constraint?
        constraints: { accept: "multipart/form-data" },
    },
    async function(request, reply) {
        // ...
    },
);

api.post(
    `/assets`,
    {
        schema: Type.Object({ fileUrl: Type.String() }),
        constraints: { accept: "application/json" },
    },
    async function(request, reply) {
        // ...
    },
);

@Fdawgs
Copy link
Member

Fdawgs commented Jun 1, 2022

Related issue: fastify/fastify#3902

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants