How to use Faker to generate dynamic responses

I read the docs at https://meta.stoplight.io/docs/prism/docs/guides/01-mocking.md which talk (only a little) about integration with Faker.js. That documentation gives a simple example, but leaves out some context. I think it is saying to modify the OpenAPI yaml file to include x-faker: name.firstName to tell the mock how to generate a dynamic value for the name property in the response. Well, I tried that and it didn’t work. So now I’m looking for more comprehensive documentation for this feature. Anyone know where I can find that?

Can you show what you have tried? It should work for the latest versions of Prism. Note that Stoplight Next does not support this functionality, but all of the latest versions do.

For example, the spec:

openapi: 3.0.0
info:
  title: Test
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  /test:
    get:
      summary: Your GET endpoint
      tags: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  first_name:
                    type: string
                    x-faker: name.firstName
      operationId: get-test
components:
  schemas: {}

Yields responses like:

{
  "first_name": "Terrance"
}

For some reason this site makes it look like someone else posted the question. Not sure why… maybe a bug to do with first time users such as myself. :slight_smile: That being said…

I took the Petstore.yml file from GitHub at https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml. I then added two lines, 95 & 96 in below yaml. Specifically the lines I added are:

x-faker: name.firstName
example: Ashleigh

Those are the only changes I made to the yaml file. I then ran Prism via Docker with this command

docker run --init --rm -it -v /Users/<username>/Tmp/OpenAPI-Specification/master/examples/v3.0/:/tmp -P stoplight/prism:4 mock -h 0.0.0.0 "/tmp/petstore.yaml"

Faker didn’t seem to work. Every response only contained “Ashleigh.” And removing the example tag didn’t make it work either. The only difference I see between your example yaml and the Petstore I am using is that you have an inline response schema whereas I have a reference to a schema. Where have I gone wrong?

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
          x-faker: name.firstName
          example: Ashleigh
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

I figured it out. I had to enable dynamic responses when running prism via docker. The correct command in my case is

docker run --init --rm -it -v /Users/<username>/Tmp/OpenAPI-Specification/master/examples/v3.0/:/tmp -P stoplight/prism:4 mock -d -h 0.0.0.0 "/tmp/petstore.yaml"

The make it more clear, I had to add -d after mock and before -h 0.0.0.0

I’m glad you figured it out! I was just coming by to suggest the dynamic flag. If you think of a way we can improve the docs to make this more clear, please send a PR. I’ll have a look myself and see if anything comes to mind.