We have a GET endpoint, which does not always return data, and the idea is to define two 2xx responses:
"paths": {
"/my-endpoint/": {
"get": {
"operationId": "myOperation",
"responses:": {
"200": {
"description": "OK with body",
"content": {
"application/json": {
"schema":{
"$ref": "#/components/schemas/MyResponse"
}
}
}
},
"204": {
"description": "OK"
}
}
....
Is this possible / allowed, and what is the best practice for such a use case?
If both cases (having and not having data) mean a standard business scenario, then I agree with your design.
Both 200 and 204 responses are pretty straightforward for the consumers, and if you add proper documentation, the API design is all right; moreover, if you use this pattern consistently in your APIs.
The final approval for such a design can be done only by knowing the resource, its purpose, and the business case.