Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save 0xdevalias/5fecf0db3bd9cc7465e42616061e1ab0 to your computer and use it in GitHub Desktop.
Save 0xdevalias/5fecf0db3bd9cc7465e42616061e1ab0 to your computer and use it in GitHub Desktop.
Exploring tools that allow converting a JSON response automagically into an OpenAPI / Swagger spec.

Converting JSON to OpenAPI / Swagger spec

Exploring tools that allow converting a JSON response automagically into an OpenAPI / Swagger spec.

Test JSON Response

{
    "accounts": {
        "default": {
            "account": {
                "account_user_role": "account-owner",
                "account_user_id": "1111aa11-a11a-1aa1-1a11-111111111111",
                "processor": {
                    "a001": {
                        "has_customer_object": true
                    },
                    "b001": {
                        "has_transaction_history": false
                    }
                },
                "account_id": "2222aa11-a11a-1aa1-1a11-111111111111",
                "is_most_recent_expired_subscription_gratis": false,
                "has_previously_paid_subscription": true
            },
            "features": [
                "data_export_enabled",
                "log_intercom_events",
                "shareable_links",
                "arkose_enabled",
                "show_existing_user_age_confirmation_modal",
                "data_controls_enabled",
                "tools2",
                "infinite_scroll_history",
                "tools3",
                "dfw_inline_message_regen_comparison",
                "log_statsig_events",
                "data_deletion_enabled",
                "new_model_switcher_20230512",
                "plugins_available",
                "dfw_message_feedback",
                "model_switcher",
                "model_preview",
                "beta_features",
                "browsing_available",
                "tools3_dev"
            ],
            "entitlement": {
                "subscription_id": "3333aa11-a11a-1aa1-1a11-111111111111",
                "has_active_subscription": true,
                "subscription_plan": "chatgptplusplan",
                "expires_at": "2023-06-12T23:39:24+00:00"
            },
            "last_active_subscription": {
                "subscription_id": "3333aa11-a11a-1aa1-1a11-111111111111",
                "purchase_origin_platform": "chatgpt_web",
                "will_renew": true
            }
        }
    },
    "temp_ap_available_at": "2023-05-20T17:30:00+00:00"
}

ChatGPT (GPT-3.5 model)

This was the response first try:

openapi: 3.0.0
info:
  title: Chat OpenAPI Specification
  version: 1.0.0

paths:
  /backend-api/accounts/check/v4-2023-04-27:
    get:
      summary: Get account information
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AccountResponse'

components:
  schemas:
    AccountResponse:
      type: object
      properties:
        accounts:
          type: object
          properties:
            default:
              $ref: '#/components/schemas/AccountDetails'
        temp_ap_available_at:
          type: string
          format: date-time

    AccountDetails:
      type: object
      properties:
        account:
          $ref: '#/components/schemas/Account'
        features:
          type: array
          items:
            type: string
        entitlement:
          $ref: '#/components/schemas/Entitlement'
        last_active_subscription:
          $ref: '#/components/schemas/LastActiveSubscription'

    Account:
      type: object
      properties:
        account_user_role:
          type: string
        account_user_id:
          type: string
          format: uuid
        processor:
          type: object
          properties:
            a001:
              $ref: '#/components/schemas/Processor'
            b001:
              $ref: '#/components/schemas/Processor'
        account_id:
          type: string
          format: uuid
        is_most_recent_expired_subscription_gratis:
          type: boolean
        has_previously_paid_subscription:
          type: boolean

    Processor:
      type: object
      properties:
        has_customer_object:
          type: boolean
        has_transaction_history:
          type: boolean

    Entitlement:
      type: object
      properties:
        subscription_id:
          type: string
          format: uuid
        has_active_subscription:
          type: boolean
        subscription_plan:
          type: string
        expires_at:
          type: string
          format: date-time

    LastActiveSubscription:
      type: object
      properties:
        subscription_id:
          type: string
          format: uuid
        purchase_origin_platform:
          type: string
        will_renew:
          type: boolean

ChatGPT (GPT-4 model)

The first response was long and naive, then I asked "Can you simplify that?" (which it did in a silly way), then I tried "Don't make it less accurate.. just use $ref and a components section and similar" which is what gave this final result (which amusingly, still seems worse than the GPT-3.5 model got first try..):

openapi: 3.0.0
info:
  title: OpenAI Account Check API
  version: 4.2023.04.27
paths:
  /backend-api/accounts/check/v4-2023-04-27:
    get:
      summary: Check account information
      operationId: checkAccount
      responses:
        '200':
          description: Account data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response'
components:
  schemas:
    Response:
      type: object
      properties:
        accounts:
          type: object
          properties:
            default:
              $ref: '#/components/schemas/Default'
        temp_ap_available_at:
          type: string
          format: date-time
    Default:
      type: object
      properties:
        account:
          $ref: '#/components/schemas/Account'
        features:
          type: array
          items:
            type: string
        entitlement:
          $ref: '#/components/schemas/Entitlement'
    Account:
      type: object
      properties:
        account_user_role:
          type: string
        account_user_id:
          type: string
        processor:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/Processor'
        account_id:
          type: string
        is_most_recent_expired_subscription_gratis:
          type: boolean
        has_previously_paid_subscription:
          type: boolean
    Processor:
      type: object
      properties:
        has_customer_object:
          type: boolean
        has_transaction_history:
          type: boolean
    Entitlement:
      type: object
      properties:
        subscription_id:
          type: string
        has_active_subscription:
          type: boolean
        subscription_plan:
          type: string
        expires_at:
          type: string
          format: date-time

SwagDefGen

definitions: 
  accounts: 
    type: object
    properties: 
      default: 
        type: object
        properties: 
          account: 
            type: object
            properties: 
              account_user_role: 
                type: string
              account_user_id: 
                type: string
              processor: 
                type: object
                properties: 
                  a001: 
                    type: object
                    properties: 
                      has_customer_object: 
                        type: boolean
                  b001: 
                    type: object
                    properties: 
                      has_transaction_history: 
                        type: boolean
              account_id: 
                type: string
              is_most_recent_expired_subscription_gratis: 
                type: boolean
              has_previously_paid_subscription: 
                type: boolean
          features: 
            type: array
            items: 
              type: string
          entitlement: 
            type: object
            properties: 
              subscription_id: 
                type: string
              has_active_subscription: 
                type: boolean
              subscription_plan: 
                type: string
              expires_at: 
                type: string
                format: date-time
          last_active_subscription: 
            type: object
            properties: 
              subscription_id: 
                type: string
              purchase_origin_platform: 
                type: string
              will_renew: 
                type: boolean
  temp_ap_available_at: 
    type: string
    format: date-time

Swagger toolbox

Utility that saves time by converting json to swagger compatible yaml models

Note: This also has an option to 'Include the example to the output', which will add example keys showing the data from the original JSON provided.

It seems that this tool doesn't try and create the complete OpenAPI spec.. just parts of it.

---
  required: 
    - "accounts"
    - "temp_ap_available_at"
  properties: 
    accounts: 
      required: 
        - "default"
      properties: 
        default: 
          required: 
            - "account"
            - "features"
            - "entitlement"
            - "last_active_subscription"
          properties: 
            account: 
              required: 
                - "account_user_role"
                - "account_user_id"
                - "processor"
                - "account_id"
                - "is_most_recent_expired_subscription_gratis"
                - "has_previously_paid_subscription"
              properties: 
                account_user_role: 
                  type: "string"
                account_user_id: 
                  type: "string"
                processor: 
                  required: 
                    - "a001"
                    - "b001"
                  properties: 
                    a001: 
                      required: 
                        - "has_customer_object"
                      properties: 
                        has_customer_object: 
                          type: "boolean"
                      type: "object"
                    b001: 
                      required: 
                        - "has_transaction_history"
                      properties: 
                        has_transaction_history: 
                          type: "boolean"
                      type: "object"
                  type: "object"
                account_id: 
                  type: "string"
                is_most_recent_expired_subscription_gratis: 
                  type: "boolean"
                has_previously_paid_subscription: 
                  type: "boolean"
              type: "object"
            features: 
              type: "array"
              items: 
                type: "string"
            entitlement: 
              required: 
                - "subscription_id"
                - "has_active_subscription"
                - "subscription_plan"
                - "expires_at"
              properties: 
                subscription_id: 
                  type: "string"
                has_active_subscription: 
                  type: "boolean"
                subscription_plan: 
                  type: "string"
                expires_at: 
                  type: "string"
              type: "object"
            last_active_subscription: 
              required: 
                - "subscription_id"
                - "purchase_origin_platform"
                - "will_renew"
              properties: 
                subscription_id: 
                  type: "string"
                purchase_origin_platform: 
                  type: "string"
                will_renew: 
                  type: "boolean"
              type: "object"
          type: "object"
      type: "object"
    temp_ap_available_at: 
      type: "string"

mock-to-openapi

type: object
properties:
  accounts:
    type: object
    properties:
      default:
        type: object
        properties:
          account:
            type: object
            properties:
              account_user_role:
                type: string
                example: account-owner
              account_user_id:
                type: string
                example: 5709ba38-b08f-4ef6-9c30-523604781473
              processor:
                type: object
                properties:
                  a001:
                    type: object
                    properties:
                      has_customer_object:
                        type: boolean
                        example: true
                  b001:
                    type: object
                    properties:
                      has_transaction_history:
                        type: boolean
                        example: false
              account_id:
                type: string
                example: 892fc33a-cdd4-49d1-88bf-108bb43b7850
              is_most_recent_expired_subscription_gratis:
                type: boolean
                example: false
              has_previously_paid_subscription:
                type: boolean
                example: true
          features:
            type: array
            items:
              type: string
              example: data_export_enabled
          entitlement:
            type: object
            properties:
              subscription_id:
                type: string
                example: 5d3ea69b-9e4d-4e0f-a930-17db43406266
              has_active_subscription:
                type: boolean
                example: true
              subscription_plan:
                type: string
                example: chatgptplusplan
              expires_at:
                type: string
                format: date-time
                example: 2023-06-12T23:39:24+00:00
          last_active_subscription:
            type: object
            properties:
              subscription_id:
                type: string
                example: 5d3ea69b-9e4d-4e0f-a930-17db43406266
              purchase_origin_platform:
                type: string
                example: chatgpt_web
              will_renew:
                type: boolean
                example: true
  temp_ap_available_at:
    type: string
    format: date-time
    example: 2023-05-20T17:30:00+00:00

Swagger Inspector

Paste your JSON object into Mocky.io, copy the URL, go to the swagger inspector and request that URL, then from the history, tick that response, and click 'Create API Definition'. It will then send the OpenAPI spec to SwaggerHub, where you can continue to work on/edit it/etc.

It looks like it basically failed to capture/convert any of the meaningful details from the response..

openapi: 3.0.1
info:
  title: defaultTitle
  description: defaultDescription
  version: '0.1'
servers:
  - url: https://run.mocky.io
paths:
  /v3/89b265dc-6d25-4d99-8087-8ee96d250097:
    get:
      description: Auto generated using Swagger Inspector
      responses:
        '200':
          description: Auto generated using Swagger Inspector
          content:
            application/json; charset=UTF-8:
              schema:
                type: string
              examples: {}
      servers:
        - url: https://run.mocky.io
    servers:
      - url: https://run.mocky.io

postman-to-openapi

openapi: 3.0.0
info:
  title: ChatGPT
  version: 1.0.0
servers:
  - url: https://chat.openai.com
paths:
  /backend-api/accounts/check/v4-2023-04-27:
    get:
      tags:
        - default
      summary: GET /backend-api/accounts/check/v4-2023-04-27
      responses:
        undefined:
          content:
            application/json:
              schema:
                type: object
              example:
                accounts:
                  default:
                    account:
                      account_user_role: account-owner
                      account_user_id: 1111aa11-a11a-1aa1-1a11-111111111111
                      processor:
                        a001:
                          has_customer_object: true
                        b001:
                          has_transaction_history: false
                      account_id: 2222aa11-a11a-1aa1-1a11-111111111111
                      is_most_recent_expired_subscription_gratis: false
                      has_previously_paid_subscription: true
                    features:
                      - data_export_enabled
                      - log_intercom_events
                      - shareable_links
                      - arkose_enabled
                      - show_existing_user_age_confirmation_modal
                      - data_controls_enabled
                      - tools2
                      - infinite_scroll_history
                      - tools3
                      - dfw_inline_message_regen_comparison
                      - log_statsig_events
                      - data_deletion_enabled
                      - new_model_switcher_20230512
                      - plugins_available
                      - dfw_message_feedback
                      - model_switcher
                      - model_preview
                      - beta_features
                      - browsing_available
                      - tools3_dev
                    entitlement:
                      subscription_id: 3333aa11-a11a-1aa1-1a11-111111111111
                      has_active_subscription: true
                      subscription_plan: chatgptplusplan
                      expires_at: '2023-06-12T23:39:24+00:00'
                    last_active_subscription:
                      subscription_id: 3333aa11-a11a-1aa1-1a11-111111111111
                      purchase_origin_platform: chatgpt_web
                      will_renew: true
                temp_ap_available_at: '2023-05-20T17:30:00+00:00'

postman2openapi

openapi: 3.0.3
info:
  title: ChatGPT
  version: 1.0.0
  contact: {}
servers:
  - url: https://chat.openai.com
paths:
  /backend-api/accounts/check/v4-2023-04-27:
    get:
      summary: GET /backend-api/accounts/check/v4-2023-04-27
      description: GET /backend-api/accounts/check/v4-2023-04-27
      operationId: getBackendApiAccountsCheckV420230427
      responses:
        '200':
          description: ''
tags: []

postman-to-openapi-online

openapi: 3.0.0
info:
  title: ChatGPT
  version: 1.0.0
servers:
  - url: https://chat.openai.com
paths:
  /backend-api/accounts/check/v4-2023-04-27:
    get:
      tags:
        - default
      summary: GET /backend-api/accounts/check/v4-2023-04-27
      responses:
        undefined:
          content:
            application/json:
              schema:
                type: object
              example:
                accounts:
                  default:
                    account:
                      account_user_role: account-owner
                      account_user_id: 1111aa11-a11a-1aa1-1a11-111111111111
                      processor:
                        a001:
                          has_customer_object: true
                        b001:
                          has_transaction_history: false
                      account_id: 2222aa11-a11a-1aa1-1a11-111111111111
                      is_most_recent_expired_subscription_gratis: false
                      has_previously_paid_subscription: true
                    features:
                      - data_export_enabled
                      - log_intercom_events
                      - shareable_links
                      - arkose_enabled
                      - show_existing_user_age_confirmation_modal
                      - data_controls_enabled
                      - tools2
                      - infinite_scroll_history
                      - tools3
                      - dfw_inline_message_regen_comparison
                      - log_statsig_events
                      - data_deletion_enabled
                      - new_model_switcher_20230512
                      - plugins_available
                      - dfw_message_feedback
                      - model_switcher
                      - model_preview
                      - beta_features
                      - browsing_available
                      - tools3_dev
                    entitlement:
                      subscription_id: 3333aa11-a11a-1aa1-1a11-111111111111
                      has_active_subscription: true
                      subscription_plan: chatgptplusplan
                      expires_at: '2023-06-12T23:39:24+00:00'
                    last_active_subscription:
                      subscription_id: 3333aa11-a11a-1aa1-1a11-111111111111
                      purchase_origin_platform: chatgpt_web
                      will_renew: true
                temp_ap_available_at: '2023-05-20T17:30:00+00:00'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment