0

I have an API that is far from standard, and I want to create Swagger docs for it. Since it's so far from standard I won't be able to generate anything, instead I'll have to do it manually.

I followed the instructions and created the "hello-world" project, I have been trying to edit this to call my own API instead. A simple GET endpoint returning a JSON list of objects.

I am assuming I can ignore everything that has to do with controllers and mocks etc since this API already returns json data if I call it through postman. I.e its a working API and all I want Swagger for is documentation.

Unfortunately I only get an "ERROR Server not found or an error occurred" when trying it out, but if I browse to the URL it's using it works fine. So I assume there is something I am missing that Swagger needs. Keep in mind I have not change any configs or anything else in this hello-world project, only the editor.

Here is my Yaml:

swagger: "2.0"
info:
  version: "0.0.1"
  title: Hello World App
# during dev, should point to your local machine
host: localhost
# basePath prefixes all resource paths 
basePath: /appname/api/v1/object
# 
schemes:
  # tip: remove http to make production-grade
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
paths:
  /campaign/:
    get:
      description: Get all campaigns
      # used as the method name of the controller
      operationId: hello
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/HelloWorldResponse"
        # responses may fall through to errors
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
# complex objects have schema definitions
definitions:
  HelloWorldResponse:
    required:
      - message
    properties:
      message:
        type: string
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

Here is the request it tries to send, which looks correct:

GET https://localhost/appname/api/v1/object/campaign/ HTTP/1.1
Host: localhost
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,fa;q=0.6,sv;q=0.4
Cache-Control: no-cache
Connection: keep-alive
Origin: http://127.0.0.1:62430
Referer: http://127.0.0.1:62430/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0
Majs
  • 607
  • 2
  • 7
  • 20
  • What's the error message in the Console tab of the browser dev tools? – Helen Jun 26 '19 at 13:30
  • @Helen Ah ofc didnt even think about looking there. It tells me I'm missing the "Access-Control-Allow-Origin" header. How can I add that to my YAML? – Majs Jun 26 '19 at 14:00
  • This is a CORS issue, it's not related to the API definition itself. You need to enable CORS on your server. https://enable-cors.org/server.html – Helen Jun 26 '19 at 14:04
  • 1
    Related (or duplicate): [How to avoid CORS errors (“Failed to fetch” or “Server not found or an error occurred”) when making requests from Swagger Editor?](https://stackoverflow.com/q/51407341/113116) – Helen Jun 26 '19 at 14:06
  • I see, thanks a lot! I'll have to take a closer look at this, didnt think that would be an issue when both the Swagger project and the API is on the same server (localhost). – Majs Jun 26 '19 at 14:10
  • From the request headers it looks like they are on different servers - the API server is `localhost` and Swagger UI is on `127.0.0.1:62430`. `localhost` != `127.0.0.1:62430`. – Helen Jun 26 '19 at 14:27
  • @Helen its funny you say that because I just logged in to ask about that. Where does "127.0.0.1:62430" come from? Is that in some config somewhere? I can see that my swagger editors URL is "http://127.0.0.1:62430/#!/". I used npm to install swagger, then did a simple "swagger project create", then "swagger project edit". And it took me to this URL. – Majs Jun 27 '19 at 07:35
  • This creates the default "hello-world" project btw. And I cannot find a reference to "127.0.0.1" in any of the generated files. – Majs Jun 27 '19 at 08:20

0 Answers0