feathersjs-ecosystem / feathers-swagger

Add documentation to your FeatherJS services and feed them to Swagger UI.
MIT License
226 stars 63 forks source link

Change or fill summary key on query #262

Closed SoxZz5 closed 3 months ago

SoxZz5 commented 3 months ago

Hey,

I tried to change the summary for my query and can't find a way to do it, I read the doc, read the lib, look like we could update summary but maybe I'm bad at it

I did something like this but looks like not working :

const heroSwaggerOptions = () =>
  createSwaggerServiceOptions({
    schemas: { heroSchema, heroQuerySchema, heroDataSchema, heroPatchSchema },
    docs: {
      description: 'Endpoint to get informations on heroes',
      securities: [],
      operations: {
        get: {
          summary: 'Test'
        },
        find: {
          summary: 'Test'
        }
      }
    },
  });

I always have this then in my json converted to yaml:

  /hero/{id}:
    get:
      parameters:
        - in: path
          name: id
          description: ID of Hero to return
          schema:
            type: integer
          required: true
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Hero'
        '401':
          description: not authenticated
        '404':
          description: not found
        '500':
          description: general error
      description: Retrieves a single resource with the given id from the service.
      summary: ''
      tags:
        - hero
      security: []

What I'm doing wrong ?

Also tried this way (ugly btw):

heroDocs.operations = {
  ...heroDocs.operations,
  find: { ...heroDocs.operations?.find, summary: 'Find all heroes' },
  get: { ...heroDocs.operations?.get, summary: 'Find hero by Id' },
}; 
Mairu commented 3 months ago

Hi @SoxZz5,

you did exactly right, by setting it via operations like you did here:

const heroSwaggerOptions = () =>
  createSwaggerServiceOptions({
    schemas: { heroSchema, heroQuerySchema, heroDataSchema, heroPatchSchema },
    docs: {
      description: 'Endpoint to get informations on heroes',
      securities: [],
      operations: {
        get: {
          summary: 'Test'
        },
        find: {
          summary: 'Test'
        }
      }
    },
  });

I have checked that this works for me and it does.

As you only show the code to create the swagger options, my question would then be, how do you use the result of it?

Or does the rest work, like your custom description and the set schemas?

SoxZz5 commented 3 months ago

I then use it in the service directly like this :

export const hero = (app: Application) => {
  app.use(heroPath, new HeroService(getOptions(app)), {
    methods: heroMethods,
    events: [],
    docs: heroDocs,
  });
  // Initialize hooks
  app.service(heroPath).hooks({
    around: {
      all: [
        schemaHooks.resolveExternal(heroExternalResolver),
        schemaHooks.resolveResult(heroResolver),
      ],
    },
    before: {
      all: [
        schemaHooks.validateQuery(heroQueryValidator),
        schemaHooks.resolveQuery(heroQueryResolver),
      ],
    },
    after: {},
    error: {},
  });
};

And swagger is init like this :

const app: Application = koa(feathers());

// Load our app configuration (see config/ folder)
app.configure(configuration(configurationValidator));

//Set up documentation custom methods handler before Koa middleware
app.configure(swagger.customMethodsHandler);

// Set up Koa middleware
app.use(cors());
app.configure(rateLimiter);

// Removed cause we don't want to have a frontend for feather
//app.use(serveStatic(app.get('public')))
app.use(errorHandler());
app.use(parseAuthentication());
app.use(bodyParser());

// Configure services and transports
app.configure(rest());

//Set up configuration for swagger
app.configure(
  swagger({
    specs: {
      info: {
        title: 'FantasyTop API',
        description: 'FantasyTop API powered by feathers',
        version: '1.0.0',
      },
    },
  })
);

app.configure(feathersCasl());
app.configure(
  socketio({
    cors: {
      origin: app.get('origins'),
    },
  })
);
app.configure(postgresql);
app.configure(services);
app.configure(channels);

// Register hooks that run on all service methods
app.hooks({
  around: {
    all: [logError],
  },
  before: {},
  after: {},
  error: {},
});
// Register application setup and teardown hooks here
app.hooks({
  setup: [],
  teardown: [],
});

export { app };

I give you all the code for better comprehension hope it would help

Btw thanks for your rapid answer

SoxZz5 commented 3 months ago

The description doesn't work too I did a try by adding this :

    docs: {
      description: 'Endpoint to get informations on heroes TGETATT',
      securities: [],
      operations: {
        find: {
          summary: 'test',
        },
        get: {
          summary: 'test',
        },
      },
tags:
  - name: hero
    description: Endpoint to get informations on heroes
Mairu commented 3 months ago

From what I see the problem could be that you create a function that returns the result instead of using it, the docs property needs to be an object.

const heroSwaggerOptions = () =>
  createSwaggerServiceOptions({
   ...
 });

const heroDocs = heroSwaggerOptions(); // Do you do this, or is this missing?

app.use(heroPath, new HeroService(getOptions(app)), {
  methods: heroMethods,
  events: [],
  docs: heroDocs,
});

I usually do

app.use(heroPath, new HeroService(getOptions(app)), {
  methods: heroMethods,
  events: [],
  docs: createSwaggerServiceOptions({ ... }),
});

but of course, it should be no problem to provide the result of createSwaggerServiceOptions in a different way.

SoxZz5 commented 3 months ago

Ok i commented customMethods and it work, but when I add back customMethods my doc options are not readed again

Update:

I found that If I instanciate customMethodsHandler before cors or other things, it fail so I assume it's not a real bug mostly me being a noob

SoxZz5 commented 3 months ago

Last update to close it, I also got a lot of problem with bun cache, when I call localhost:3030 that's ok, but when I call my generate script I got problem, but no worry it's fixed on my side thanks for your fast answer,

I will have many other question in the futur I pushed my doc on redocly and I'm not really feeling happy with what it generated but it maybe also about how I used feathers, so before asking will try to figure out