apicollective/apibuilder-generator

Mocks should respect string length restrictions

Closed this issue · 3 comments

When you write { "name": "currency", "minimum": 3, "maximum": 3 }, the mock clients should only generate strings of length 3.

One solution is that Factories.randomString() could take an optional length parameter, and then the factory will pass the minimum length.

this is a good idea that we should do asap - should be a small change - want to take a stab?

mkows commented

@benwaffle please see my proof of concept #515. Should do the job.

Please comment on TODO and comments in code.

mkows commented

Please take a brief look when you get a chance @benwaffle @mbryzek @gheine

Run locally, and for https://app.apibuilder.io/michal/playground-api/0.0.2-dev#model-error
(raw: http://localhost:9000/michal/playground-api/0.0.2-dev/original)
api.json:

    "error": {
      "fields": [
        {
          "name": "code",
          "type": "integer"
        },
        {
          "name": "message",
          "type": "string",
          "maximum": 12
        },
        {
          "name": "message_code",
          "type": "string",
          "minimum": 2,
          "maximum": 2
        },
        {
          "name": "message_lengthy",
          "type": "string",
          "minimum": 30
        },
        {
          "name": "details",
          "type": "string",
          "required": false
        }
      ]
    },
    "healthcheck": {
      "fields": [
        {
          "name": "status",
          "type": "string",
          "example": "healthy",
          "minimum": 3
        },
        {
          "name": "created_at",
          "type": "date-time-iso8601",
          "example": "2014-04-29T11:56:52Z"
        }
      ]
    }

the output is:
Play 2.6:

  object Factories {

    def randomString(length: Int = 24): String = {
      _root_.scala.util.Random.alphanumeric.take(length).mkString
    }

    def makeError(): io.github.mkows.playground.api.v0.models.Error = io.github.mkows.playground.api.v0.models.Error(
      code = 1,
      message = Factories.randomString(12),
      messageCode = Factories.randomString(2),
      messageLengthy = Factories.randomString(30),
      details = None
    )

    def makeHealthcheck(): io.github.mkows.playground.api.v0.models.Healthcheck = io.github.mkows.playground.api.v0.models.Healthcheck(
      status = Factories.randomString(24),
      createdAt = org.joda.time.DateTime.now
    )

Http4s 0.20

  object Factories {

    def randomString(length: Int = 24): String = {
      _root_.scala.util.Random.alphanumeric.take(length).mkString
    }

    def makeError(): io.github.mkows.playground.api.v0.models.Error = io.github.mkows.playground.api.v0.models.Error(
      code = 1,
      message = Factories.randomString(12),
      messageCode = Factories.randomString(2),
      messageLengthy = Factories.randomString(30),
      details = None
    )

    def makeHealthcheck(): io.github.mkows.playground.api.v0.models.Healthcheck = io.github.mkows.playground.api.v0.models.Healthcheck(
      status = Factories.randomString(24),
      createdAt = java.time.Instant.now
    )

PR: #515 (with some TODO comments -- mostly around supporting randomString(length = Int.MaxValue | Long.MaxValue) 😨 which should be rejected).