Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Return Swagger JSON from backend service for custom route like /docs

Not applicable

As far as I know it's not possible out of the box to return the Swagger JSON in Apigee. Although that Swagger has been used to generate the proxy, it's not possible to return the contents of that file to the caller.

I'm looking for a way to return the Swagger JSON to the caller, so I created a proxy flow which is triggered by a GET for /docs pattern.

I thought it would (or could) be sufficient to use the AssignMessage policy to rewrite the URL to the backend service swagger, by using the 'AssignVariable' setting:

<AssignVariable>
  <Name>target.url</Name>
  <Value>https://some-service.com/docs/v1/swagger.json</Value>
  <Ref/>
</AssignVariable>

But this doesn't appear to work?

When I look at the trace window I see the target.url is populated with my url but it's not used for accessing my backend service?

Is there anything else I need to do to accomplish this?

Solved Solved
2 6 1,592
1 ACCEPTED SOLUTION

Not applicable

In the end I managed to solve it using this approach:

1) Add a 'Swagger' flow in the proxy flow

<Flow name="Swagger">
  <Request/>
  <Response/>
  <Condition>
    (proxy.pathsuffix MatchesPath "/api/v1.0/docs") and (request.verb = "GET")
  </Condition>
</Flow>

2) Add an Assign Message policy to set the target.url to the swagger location, containing this part

<AssignVariable>
  <Name>target.url</Name>
  <Value>https://some-backendservice.com/docs/v1/swagger.json</Value>
  <Ref/>
</AssignVariable>

3) Add a 'Swagger' flow in the target flow

<Flow name="Swagger">
  <Request>
    <Step>
      <Name>Set-Swagger-Endpoint</Name>
    </Step>
  </Request>
  <Condition>
    (proxy.pathsuffix MatchesPath "/api/v1.0/docs") and (request.verb = "GET")
  </Condition>
</Flow>

It would be great when I didn't need to specify the Swagger endpoint in the Assign Message policy, but could use the <spec>.....</spec> entry in the ApiProxy definition instead, as this also contains the endpoint.

View solution in original post

6 REPLIES 6