raml 0.8并使用特征进行消息体

时间:2018-03-27 09:44:31

标签: traits raml

我正在使用RAML 0.8,我正在尝试使用/了解特征

我有一个api设计,我可以看到每个api调用在消息体中重复了几个字段。而我对raml规范的解读是因为这些是可重复的,它们应该是特征的候选者

/kmi/for/{product}:
  uriParameters: 
    product: 
      description: |
        The product that want to be informed about
      required: true
      repeat: false
      type: string
      example: beans
  post:
    description: |
      Used to submit a keep me informed request
    body:
      application/x-www-form-urlencoded:
        formParameters:
          title:
            description: ...
            type: string
            required: false
            repeat: false
            example: Mr
          firstname:
            description: ...
            type: string
            required: true
            repeat: false
            example: John
          lastname:
            description: ...
            type: string
            required: true
            repeat: false
            example: Connor
          emailAddress:
            description: ...
            type: string
            required: true
            repeat: false
            example: john.connor@sky.net

现在我要做的是将表单字段,标题,名字,姓氏,电子邮件地址提取到特征中。

我最初尝试过以下

traits:
  - minimumFormFields:
      title:
        description: ...
        type: string
            ....
      firstname:
        description: ...
        type: string
            ....
      lastname:
        description: ...
        type: string
            ....
      emailAddress:
        description: ...
        type: string
            ....

并添加到特征中(为简单起见,删除了额外的字段)

/kmi/for/{product}:
  post:
    description: |
      ...
    body:
      application/x-www-form-urlencoded:
        formParameters:
          is: [minimumFormFields]

然而,这给了我错误:

  

RAML 0.8不允许使用'标题'性状内的财产

我尝试更新特征以包含原始定义中的更多内容,但我所做的一切似乎都无法正常工作

traits:
  - minimumFormFields:
      application/x-www-form-urlencoded:
        formParameters:
          title:
            description: ...
            type: string
            ....
          firstname:
            description: ...
            type: string
            ....
          lastname:
            description: ...
            type: string
            ....
          emailAddress:
            description: ...
            type: string
            ....

但是这给了我以下错误

  

[错误]未知节点:' application / x-www-form-urlencoded'

如果我然后将主体包含在特征中,那么我会收到以下错误

  

[错误]未知节点:'' [错误]无法识别的特征:   ' minimumFormFields'

如何将作为邮件正文一部分提交的字段提取到特征?

我们公司已经对RAML 0.8进行了标准化,因此我无法升级到RAML 1。

** *更新1 * **

回顾Petru Gardea的评论我创建了一种资源类型

resourceTypes: 
  minimumFormFields:
    post?:
      body:
        application/x-www-form-urlencoded:
          formParameters:
            title:
              description: ...
              type: string
              required: true
              repeat: false

但我仍然遇到错误

  

[error]节点:' minimumFormFields'应按顺序包裹   [错误]未知节点:' minimumFormFields'

改为 -minimumFormFields:

  

[error]节点:' -minimumFormFields'应按顺序包裹   [错误]未知节点:' -minimumFormFields'

改为 - minimumFormFields:

  

[错误]未知节点:''

我正在使用mulesoft.com编辑我的RAML

1 个答案:

答案 0 :(得分:0)

我认为你不能用特质达到你想要的效果。相反,您需要使用resourceTypes。

这是一种可能的方法,向您展示它是如何工作的。资源的含义如下:

  • / kmi / test / 1:即使它是minimumFormFields类型,因为它没有定义帖子,它也不会从resourceType“继承”它(注意帖子之后的帖子,这使它成为可选的);
  • / kmi / test / 2:根据原始帖子定义为参考
  • / kmi / test / 3:表示您甚至可以从“基础”类型覆盖/添加。
  • / kmi / for / {product}:根据您的要求进行清洁设置。

能够“可视化”“已解决”的合同总是有帮助的。 RAML解析器应该为您执行此操作。 RAML之后的屏幕截图应该是自我解释的。

#%RAML 0.8
---
title: Sample
resourceTypes: 
  - minimumFormFields:
      post?:      
        body:
          application/x-www-form-urlencoded:   
            formParameters:
              title:
                description: ...
                type: string
                required: false
                repeat: false
                example: Mr
              firstname:
                description: ...
                type: string
                required: true
                repeat: false
                example: John
              lastname:
                description: ...
                type: string
                required: true
                repeat: false
                example: Connor
              emailAddress:
                description: ...
                type: string
                required: true
                repeat: false
                example: john.connor@sky.net            

/kmi/test/1:
  type: minimumFormFields
  uriParameters: 
    product: 
      description: |
        The product that want to be informed about
      required: true
      repeat: false
      type: string
      example: beans
  get:
    description: |
      Doens't show a POST
/kmi/test/2:
  post:
    description: |
      Used to submit a keep me informed request
    body:
      application/x-www-form-urlencoded:
        formParameters:
          title:
            description: ...
            type: string
            required: false
            repeat: false
            example: Mr
          firstname:
            description: ...
            type: string
            required: true
            repeat: false
            example: John
          lastname:
            description: ...
            type: string
            required: true
            repeat: false
            example: Connor
          emailAddress:
            description: ...
            type: string
            required: true
            repeat: false
            example: john.connor@sky.net        
/kmi/for/{product}:
  type: minimumFormFields
  uriParameters: 
    product: 
      description: |
        The product that want to be informed about
      required: true
      repeat: false
      type: string
      example: beans
  post:
    description: |
      Used to submit a keep me informed request
/kmi/test/3:
  type: minimumFormFields
  uriParameters: 
    product: 
      description: |
        The product that want to be informed about
      required: true
      repeat: false
      type: string
      example: beans
  post:
    description: |
      Used to submit a keep me informed request      
    body:
      application/x-www-form-urlencoded:
        formParameters:
          anotherThing:
            description: ...
            type: string
            required: false
            repeat: false
            example: Mr
          emailAddress:
            description: This looks better
            type: string
            required: true
            repeat: false
            example: johnny.connor@sky.net

测试1 :删除POST,仅显示GET

enter image description here

测试2 :“内联”方式。

enter image description here

测试3 :“重复使用”方式。

enter image description here

测试4 :“被覆盖”的方式。

codepen