3

My swagger.yaml is working fine with the only issue being the cache-control header. I don't see it in the response. I have defined it as below. Could you please let me know what is wrong with it?

/{empId}/class:
get:
  tags:
  - "School"
  summary: "Some Summary"
  description: "Some description"
  operationId: "getDataForEmp"
  produces:
  - "application/json;charset=UTF-8"
  parameters:
  - name: "empId"
    in: "path"
    description:  "The emp id for which record is to be fetched."
    required: true
    type: "string"
  responses:
    200:
      description: "Data retrieved successfully."
      schema:
        $ref: "#/definitions/MyResponse"
      headers:
        Cache-Control:
          default:  "public, max-age=43200"
    401:
      description: "You are not authorised to make this change."
    403:
      description: "Accessing the resource you were trying to reach is forbidden."
    404:
      description: "The resource you were trying to reach is not found."
T Anna
  • 874
  • 5
  • 21
  • 52
  • What do you mean by "don't see it [Cache-Control] in the response"? A) This header is not displayed in Swagger UI (or whatever API doc tool you use). B) Your server does not return this header in responses. C) Something else? – Helen Feb 04 '19 at 15:38
  • You also need to add `type: string` to the Cache-Control header definition to make it valid. – Helen Feb 04 '19 at 15:41
  • @Helen When i hit the endpoint using postman, i don't see the cache-control header in the response. – T Anna Feb 04 '19 at 15:48
  • See if this helps: [How to enable HTTP response caching in Spring Boot](https://stackoverflow.com/q/24164014/113116) – Helen Feb 04 '19 at 15:53
  • @Helen I added the type as well, but that also didn't work.Neither I can see it in the generated java code interface in the endpoint definition, nor when i run it, postman doesn't return it in the response either. – T Anna Feb 04 '19 at 15:54
  • If I try to add cache-control header in my java code in the controller method, it works and I can see it in postman. It is not working when I'm trying to achieve the same using swagger.yaml. – T Anna Feb 04 '19 at 15:55
  • Looks like the server-side framework/library you're using cannot add caching automatically based on the `Cache-Control` header definition alone. Consider filing a ticket with the developers of that framework/library. – Helen Feb 04 '19 at 17:08
  • Yes, I thought so too. Thanks for taking a look though. – T Anna Feb 04 '19 at 20:30

0 Answers0