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

Swagger doesn't work with Reference Models #49

Open
lomithrani opened this issue Oct 20, 2023 · 1 comment
Open

Swagger doesn't work with Reference Models #49

lomithrani opened this issue Oct 20, 2023 · 1 comment

Comments

@lomithrani
Copy link

When using reference models with swagger we hit a NOT_FOUND error (elysia side)
and Resolver error at paths./experiences.post.requestBody.content.application/json.schema.$ref Could not resolve reference: Could not resolve pointer: /components/schemas/experienceRequest does not exist in document (Swagger side)

image

models/elysia/experienceRequest.models.ts

import Elysia, { t } from "elysia";

export const experienceRequest = new Elysia()
  .model({
    'experienceRequest': t.Object({
      type: t.String(),
      title: t.String(),
      summary: t.String(),
    })
  })

plugins/experiences.ts

import { Domain, Experience } from 'models/database';
import { experienceRequest } from 'models/elysia'
import { Elysia } from 'elysia';
import { corsConf } from './corsConf';
import { userLogged } from './userLogged';
import { CannotSaveExperienceError, DomainDoesNotExistError } from 'errors';

export const experiences = new Elysia()
  .use(corsConf())
  .use(experienceRequest)
  .get('/experiences', async () => await Experience.find())
  .use(userLogged)
  .post(
    '/experiences',
    async ({ body, userId }) => {

      const domain = await Domain.findOne({ admin: userId });

      if (!domain) throw new DomainDoesNotExistError(userId)

      let experience = new Experience()
      experience.title = body.title
      experience.summary = body.summary
      experience.type = body.type

      const result = await experience.save();

      if (!result) throw new CannotSaveExperienceError(userId)

      domain.experiences.push(result.id)

      await domain.save()

      return result

    },
    {
      body: 'experienceRequest',
      detail: {
        summary: 'Add new experience'
      }
    }
  )

index.ts

import mongoose from 'mongoose';
import { Elysia } from 'elysia'
import { experiences, googleAuth, domain } from 'plugins';
import { validateEnvironment } from './services/validation';
import swagger from '@elysiajs/swagger';
import { errors } from 'errors';

validateEnvironment();

await mongoose.connect(Bun.env.MONGO_URL ?? '');

const app = new Elysia()
  .use(swagger())
  .error(errors)
  .onError(({ code, error }) => {
    console.error(error)
    return error.message;
  })
  .use(domain)
  .use(googleAuth)
  .use(experiences)
  .listen({
    hostname: Bun.env.HOSTNAME || "::",
    port: Bun.env.PORT || 3000,
    tls: Bun.env.TLS_PASSPHRASE ? {
      cert: Bun.file('./cert.pem'),
      key: Bun.file('./key.pem'),
      passphrase: Bun.env.TLS_PASSPHRASE
    } : undefined
  })

console.log(`Running at http://${app.server!.hostname}:${app.server!.port} CORS allowed: ${JSON.stringify(Bun.env.ALLOWED_DOMAINS)}`)

export type Portfolio = typeof app;
@lomithrani
Copy link
Author

lomithrani commented Oct 22, 2023

Actually this also breaks edenTreaty so the problem might be somewhere else.
Explicitly type the body with :

t.Object({
      type: t.String(),
      title: t.String(),
      summary: t.String(),
    })

works the above implementation doesnt

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

No branches or pull requests

1 participant