RAML中的#ref与Mule(apikit)

时间:2016-03-01 09:12:56

标签: mule raml

我们使用的是Mule 3.7 CE。在我们的Maven POM中,我们使用apikit 1.7.3。我们还使用raml-for-jax-rs来生成基于RAML的java代码。

我们要做的是在代码方面使用一个公共对象,raml / jsonschema-wise引用各种其他模式可以使用的公共模式。

mule-flow使用apikit并链接到以下RAML(仅作为示例):

#%RAML 0.8
version: v1.0
title: Test service
mediaType: application/json
schemas:
  - addressbook: |
      {
        "$schema": "draft4 (normally the url needs to be here but can't post more then 2 links)",
        "title": "addressbook",
        "description": "addressbook object",
        "type": "object",
        "properties": {
          "address": {
            "description": "address",
            "type": "string",
            "default":""
          }
        }
      }
  - testinput1: |
      {
        "$schema": "draft4 (normally the url needs to be here but can't post more then 2 links)",
        "title": "Testinput1",
        "description": "Testinput1 object",
        "type": "object",
        "properties": {
          "name": {
            "description": "name",
            "type": "string",
            "default":""
          },
          "addressbook": {
            "$ref" : "addressbook"
          }
        }
      }
  - testinput2: |
      {
        "$schema": "draft4 (normally the url needs to be here but can't post more then 2 links)",
        "title": "Testinput2",
        "description": "Testinput2 object",
        "type": "object",
        "properties":
        {
          "name": {
            "description": "id",
            "type": "string"
          },
          "addressbook": {
            "$ref" : "addressbook"
          }
        }
      }
  - output: |
      {
        "$schema": "draft4 (normally the url needs to be here but can't post more then 2 links)",
        "title": "Output",
        "description": "Output object",
        "type": "object",
        "properties": {
          "code": {
            "description": "code",
            "type": "string",
            "default":""
          }
        }
      }
/test1:
  displayName: test1
  post:
    description: test1
    body:
      application/json:
        schema: testinput1
    responses:
      200:
        body:
          application/json:
            schema: output
/test2:
  displayName: test2
  post:
    description: test2
    body:
      application/json:
        schema: testinput2
    responses:
      200:
        body:
          application/json:
            schema: output

现在问题是我们在运行时执行POST时出现以下错误:

 ERROR 2016-02-29 15:45:52,755 [[testraml].test-httpListenerConfig.worker.01] org.mule.module.apikit.MappingExceptionListener: 
********************************************************************************
Message               : fatal: URI "addressbook#" is not absolute
    level: "fatal"
    uri: "addressbook#"
 (com.github.fge.jsonschema.core.exceptions.ProcessingException)
Type                  : org.mule.module.apikit.exception.BadRequestException
Code                  : MULE_ERROR--2
JavaDoc               : http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/apikit/exception/BadRequestException.html
********************************************************************************
Exception stack is:
1. fatal: URI "addressbook#" is not absolute
    level: "fatal"
    uri: "addressbook#"
 (com.github.fge.jsonschema.core.exceptions.ProcessingException)
  com.github.fge.jsonschema.core.load.SchemaLoader:163 (null)
2. fatal: URI "addressbook#" is not absolute
    level: "fatal"
    uri: "addressbook#"
 (com.github.fge.jsonschema.core.exceptions.ProcessingException) (org.mule.module.apikit.exception.BadRequestException)
  org.mule.module.apikit.validation.RestJsonSchemaValidator:95 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/apikit/exception/BadRequestException.html)
********************************************************************************
Root Exception stack trace:
com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: URI "addressbook#" is not absolute
    level: "fatal"
    uri: "addressbook#"

    at com.github.fge.jsonschema.core.load.SchemaLoader.get(SchemaLoader.java:163)
    at com.github.fge.jsonschema.core.load.RefResolver.rawProcess(RefResolver.java:114)
    at com.github.fge.jsonschema.core.load.RefResolver.rawProcess(RefResolver.java:51)
    at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:77)
    at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:41)
    at com.github.fge.jsonschema.core.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:189)
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:128)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:120)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
    at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:109)
    at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:107)
    at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:57)
    at com.github.fge.jsonschema.core.processing.ProcessorMap$Mapper.process(ProcessorMap.java:166)
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:128)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:120)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
    at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:109)
    at com.github.fge.jsonschema.processors.validation.InstanceValidator.process(InstanceValidator.java:136)
    at com.github.fge.jsonschema.processors.validation.InstanceValidator.processObject(InstanceValidator.java:241)
    at com.github.fge.jsonschema.processors.validation.InstanceValidator.process(InstanceValidator.java:176)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:56)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:34)
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:79)
    at com.github.fge.jsonschema.main.JsonSchema.doValidate(JsonSchema.java:76)
    at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:109)
    at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:125)
    at org.mule.module.apikit.validation.RestJsonSchemaValidator.validate(RestJsonSchemaValidator.java:73)
    at org.mule.module.apikit.HttpRestRequest.validateSchema(HttpRestRequest.java:413)
    at org.mule.module.apikit.HttpRestRequest.valideateBody(HttpRestRequest.java:312)
    at org.mule.module.apikit.HttpRestRequest.negotiateInputRepresentation(HttpRestRequest.java:289)
    at org.mule.module.apikit.HttpRestRequest.validate(HttpRestRequest.java:114)
    at org.mule.module.apikit.AbstractRouter.processRouterRequest(AbstractRouter.java:193)
    at org.mule.module.apikit.AbstractRouter.processBlockingRequest(AbstractRouter.java:89)
    at org.mule.module.apikit.AbstractRouter.processBlocking(AbstractRouter.java:83)
    at org.mule.processor.AbstractRequestResponseMessageProcessor.process(AbstractRequestResponseMessageProcessor.java:47)
    at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
    at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:107)
    at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
    at org.mule.processor.BlockingProcessorExecutor.executeNext(Blocking...
********************************************************************************

来自raml-for-jax-rs的生成代码看起来不错。我们得到了一个TestInput1和TestInput2使用的Addressbook对象。

我们确实找到了一张jira票据,说明它已在1.7.3(我们在POM中使用)中修复。据我们所知,这个问题似乎仍未得到解决,因为RAML方面看起来不错,代码方面它看起来不错,但使用带有API-kit的RAML会产生错误。

我们做错了什么?这仍然是一个错误吗?我们需要等待Mule 3.8吗?任何解决方法?我们在概念层面做错了吗?

同样在旁注;查看APIkit控制台会显示完整的引用,而不是描述引用。这样外部开发人员就不知道该方案的上下文是什么。有没有解决方法呢?

1 个答案:

答案 0 :(得分:1)

我记得我使用自定义JSON验证器来调用json架构,但是你的情况是从不同架构引用对象并生成多个架构。

您可以创建多个架构文件,然后使用raml!include吗?说!包括sampleschema.json。

然后检查链接:http://spacetelescope.github.io/understanding-json-schema/structuring.html

那里有一个部分:

“井号符号(#)指的是当前文档,然后斜杠(/)分隔的键只是遍历文档中对象的键。因此,在我们的示例中,”#/ definitions / address“表示:

转到文档的根目录 找到关键字“定义”的值 在该对象中,找到键“地址”的值 $ ref也可以是相对URI或绝对URI,因此如果您希望将定义包含在单独的文件中,也可以这样做。例如:

{“$ ref”:“definitions.json#/ address”}“