open-traffic-generator / openapiart

OpenAPI artifact generator
MIT License
6 stars 4 forks source link

add support for random UDF for x-field-pattern fields #467

Closed Vibaswan closed 1 month ago

Vibaswan commented 2 months ago

Introducing New random feature for x-field-pattern

Random for Ipv4 Pattern

# initial definition
ipv4:
# other parameters
 dst:
  x-field-pattern:
    format: ipv4
    default: 0.0.0.0
    features: [random]
  x-field-uid: 1

# after expansion by openapiart
Pattern.Ipv4Pattern.Ipv4.Dst:
  type: object
  properties:
    choice:
      type: string
      x-enum:
        value:
          x-field-uid: 2
        values:
          x-field-uid: 3
        random:
          x-field-uid: 6
      default: value
      x-field-uid: 1
      enum:
      - value
      - values
      - random
    value:
      type: string
      x-field-uid: 2
      default: 0.0.0.0
      format: ipv4
    values:
      type: array
      items:
        type: string
        format: ipv4
      x-field-uid: 3
      default:
      - 0.0.0.0
    random:
     $ref: '#/components/schemas/Pattern.Ipv4Pattern.Ipv4.Dst.Random'
     x-field-uid: 4

Pattern.Ipv4Pattern.Ipv4.Dst.Random:
  description: |-
    ipv4 random pattern
  type: object
  properties:
    min:
      type: string
      x-field-uid: 1
      default: 0.0.0.0
      format: ipv4
    max:
      type: string
      x-field-uid: 2
      default: 255.255.255.255
      format: ipv4
    seed:
      type: integer
      x-field-uid: 3
      default: 1
      format: uint32
    count:
      type: integer
      x-field-uid: 4
      default: 1
      format: uint32

Random for mac pattern

# initial definition
mac:
# other parameters
 dst:
  x-field-pattern:
    format: mac
    features: [random]
  x-field-uid: 1

# after expansion by openapiart
# skipping intermediate strcuture

Pattern.MacPattern.Mac.Random:
  description: |-
    mac random pattern
  type: object
  properties:
    min:
      type: string
      x-field-uid: 1
      default: 00:00:00:00:00:00
      format: mac
    max:
      type: string
      x-field-uid: 2
      default: ff:ff:ff:ff:ff:ff
      format: mac
    seed:
      type: integer
      x-field-uid: 3
      default: 1
      format: uint32
    count:
      type: integer
      x-field-uid: 4
      default: 1
      format: uint32

Random for integer pattern

# initial definition
integer_pattern:
# other parameters
 integer:
  x-field-pattern:
    format: integer
    default: 0
    length: 8
    features: [random]
  x-field-uid: 1

# after expansion by openapiart
# skipping intermediate structure

Pattern.IntegerPattern.Integer.Random:
  description: |-
    integer random pattern
  type: object
  properties:
    min:
      type: integer
      x-field-uid: 1
      default: 0
      format: uint32
      maximum: 255
    max:
      type: integer
      x-field-uid: 2
      default: 255
      format: uint32
      maximum: 255
    seed:
      type: integer
      x-field-uid: 3
      default: 1
      format: uint32
      maximum: 255
    count:
      type: integer
      x-field-uid: 4
      default: 1
      format: uint32
      maximum: 255

sample python code to use random

random = config.ipv4.dst.random
random.min = "1.2.3.4"
random.max = "255.255.3.4"
random.count = 2
random.seed = 2

sample go code to use random

r := Config.Ipv4().Dst().Random()
r.SetMin("1.2.3.4").SetMax("255.255.3.4").SetCount(2).SetSeed(2)