Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Does the file-based mock middleware support cross-file referencing? #173

Closed
acsor opened this issue Oct 16, 2020 · 1 comment
Closed

Does the file-based mock middleware support cross-file referencing? #173

acsor opened this issue Oct 16, 2020 · 1 comment

Comments

@acsor
Copy link

acsor commented Oct 16, 2020

Hi, I couldn't find any hint as to what I'm asking in the Mock middleware doc, so I felt obliged to post. I'm basically asking myself whether the Mock middleware possesses a certain feature.

The prototype of an application I'm writing has the following types of resources: Story, Mission and Activity. A single story contains multiple missions, which in turn may contain many activities. My intent so far has been to use a file-based DB and store all the individual collections in separate .json files (actually this is taken care of by the Mock middleware) and have, by way of example, a single Story contain a reference to one or more activities, not an actual JSON payload to the same data (the natural way to achieve this might be to use integer identifiers).

I've been inspecting the PetStore.yaml file and the way it is integrated in Sample 1 and what I've seen is that if a resource contains a reference to another resource, the latter is stored not "by reference" (e.g. a numerical id) but "by value" (an entire JSON structure embodied within).

Does the Mock middleware possess this kind of feature, in such a way that I can GET /stories/0/missions for example, or do I need to implement this mechanism by myself?

@acsor
Copy link
Author

acsor commented Oct 16, 2020

Addenda

For the Sample 1 code to work with a file-based database it was necessary a small tweak by me, which wasn't included by default.

Source code

If my question above didn't sound completely clear to you, I'll try to paste some of the .yaml code I've written. Please note that I don't maintain a single Swagger file, but many which do cross-referencing.

swagger.yaml

swagger: '2.0'

info:
  version: '0.1.0'
  title: 'M&M Rest Server'
  description: 'M&M REST access server.'

consumes:
  - application/json
produces:
  - application/json

paths:
  /stories:
    $ref: stories.yaml#/stories
  /stories/{id}:
    $ref: stories.yaml#/stories.id
  /stories/{id}/missions:
    $ref: stories.yaml#/stories.id.missions
  /missions:
    $ref: missions.yaml#/missions
  /missions/{id}:
    $ref: missions.yaml#/missions.id

definitions.yaml

Story:
  type: object
  required:
    - name
  properties:
    name:
      type: string
    description:
      type: string
    missions:
      type: array
      items:
        $ref: '#/Mission'

Mission:
  type: object
  required:
    - name
  properties:
    name:
      type: string
    description:
      type: string
    activities:
      type: array
      items:
        $ref: '#/Activity'

Activity:
  type: object
  required:
    - name
  properties:
    name:
      type: string
    description:
      type: string

stories.yaml

stories:
  get:
    summary: 'Retrieves a list of all the stories'
    operationId: 'getStories'
    responses:
      200:
        description: 'Successfully retrieved stories'
        schema:
          type: array
          items:
            $ref: definitions.yaml#/Story
      400:
        $ref: responses.yaml#/HTTP400
  post:
    summary: 'Creates a new story'
    operationId: 'createStory'
    parameters:
      - in: body
        name: 'story'
        description: 'New story to create'
        schema:
          $ref: definitions.yaml#/Story
    responses:
      201:
        $ref: responses.yaml#/HTTP201
        schema:
          $ref: definitions.yaml#/Story
stories.id:
  parameters:
    - $ref: parameters.yaml#/id
  get:
    operationId: 'getStory'
    summary: 'Retrieves a specific story identified by the path ID'
    responses:
      200:
        $ref: responses.yaml#/HTTP200
        schema:
          $ref: definitions.yaml#/Story
      404:
        $ref: responses.yaml#/HTTP404
    # TODO Add patch method
stories.id.missions:
  parameters:
    - $ref: parameters.yaml#/id
  get:
    description: 'Retrieves all missions related to a given story'
    responses:
      200:
        $ref: responses.yaml#/HTTP200
        schema:
          type: array
          items:
            $ref: definitions.yaml#/Mission
      404:
        $ref: responses.yaml#/HTTP404
  post:
    description: 'Associates a new mission to this story'
    responses:
      201:
        $ref: responses.yaml#/HTTP201
        schema:
          type: array
          item:
            $ref: definitions.yaml#/Mission

missions.yaml etc.

Omitted for brevity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants