oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Support for application/json and application/yaml

Open dkrizic opened this issue 2 years ago • 2 comments

Would it be possible to extend the StrictServerInterface to support something like this in an automated way:

      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Component'
            application/yaml:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Component'

The implementation would then only return one generic response type that gets marshalled depending on the "Accept" header

func (o OrchestratorServer) GetComponents(ctx context.Context, request server.GetComponentsRequestObject) (server.GetComponentsResponseObject, error) {
	log.Info("GetComponents")
	components, err := o.componentRepository.GetAll()
	return server.GetComponents200JSONResponse(components), nil
}

So instead of returning a GetCompoenent200JSONResponse to return a more generic object

dkrizic avatar Jun 29 '23 16:06 dkrizic

Currently, in the strict server interface, there is no way to retrieve the value of the "Accept" header and provide an appropriate response, such as *JSONResponse or *HTMLResponse. The function FindPets() in the following example takes a FindPetsRequestObject, which does not include any HTTP headers.

https://github.com/deepmap/oapi-codegen/blob/dd082985a9b6e8f68472987f6c2c60fea5e59871/examples/petstore-expanded/strict/api/petstore-server.gen.go#L428

As a result, it is not feasible to return either FindPets200JSONResponse or FindPets200HTMLResponse correctly based on the value of the "Accept" header. Is there a way to implement multiple types of responses in the strict server interface?

wjkoh avatar Mar 12 '24 05:03 wjkoh

There is a workaround available at https://github.com/deepmap/oapi-codegen/issues/1322, however, it may not be the most intuitive way to handle this fundamental functionality, in my opinion. Would @Warboss-rus or any others involved in the strict server interface be able to provide guidance on this matter?

wjkoh avatar Mar 12 '24 05:03 wjkoh