生成swagger Java客户端代码时出错

时间:2018-11-12 03:12:54

标签: java swagger swagger-codegen

我正在尝试使用以下命令生成Java客户端代码

java -jar swagger-codegen-cli-3.0.2.jar generate -v  \
        -i swagger.json \
        -l java

但出现以下错误

java.util.NoSuchElementException: No value present
    at java.util.Optional.get(Optional.java:135)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.configureDataForTestTemplate(DefaultCodegenConfig.java:3766)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2107)
    at io.swagger.codegen.v3.generators.java.AbstractJavaCodegen.fromOperation(AbstractJavaCodegen.java:1089)
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:847)
    at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:771)
    at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:419)
    at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:722)
    at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
    at java.lang.Thread.run(Thread.java:748)
Exception in thread "Thread-1" java.lang.RuntimeException: Could not process operation:
    Tag: class Tag {
    name: DIMSE-RS
    description: Invoke DIMSE Service on remote AE
    externalDocs: null
    }
Operation: SearchForStudySeriesInstancesExternal
Resource: get /aets/{aet}/dimse/{externalAET}/studies/{StudyInstanceUID}/series/{SeriesInstanceUID}/instances
Exception: No value present
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:872)
    at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:771)
    at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:419)
    at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:722)
    at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.NoSuchElementException: No value present
    at java.util.Optional.get(Optional.java:135)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.configureDataForTestTemplate(DefaultCodegenConfig.java:3766)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2107)
    at io.swagger.codegen.v3.generators.java.AbstractJavaCodegen.fromOperation(AbstractJavaCodegen.java:1089)
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:847)
    ... 5 more

swagger.json文件的以下部分

"/aets/{aet}/dimse/{externalAET}/studies/{StudyInstanceUID}/series/{SeriesInstanceUID}/instances": {
  "get": {
    "tags": [
      "DIMSE-RS"
    ],
    "summary": "Search for Instances of Series of Study external",
    "operationId": "SearchForStudySeriesInstancesExternal",
    "produces": [
      "application/dicom+json",
      "application/json"
    ],
    "parameters": [
      {
        "name": "aet",
        "in": "path",
        "description": "Archive AE Title used as Calling AET for querying for Instances",
        "required": true,
        "type": "string"
      },
      {
        "name": "externalAET",
        "in": "path",
        "description": "AE Title of C-FIND SCP to query for Instances",
        "required": true,
        "type": "string"
      },
      {
        "name": "SeriesInstanceUID",
        "in": "path",
        "description": "Series Instance UID for a single Series",
        "required": true,
        "type": "string"
      },
      {
        "name": "priority",
        "in": "query",
        "description": "requested priority: 0=NORMAL, 1=HIGH, 2=LOW",
        "type": "integer",
        "enum": [ 0, 1, 2 ],
        "default": 0
      },
      {
        "name": "{attributeID}",
        "in": "query",
        "description": "{attributeID}={value}; {attributeID} = {dicomTag} | {dicomKeyword} | {dicomTag}.{attributeID} | {dicomKeyword}.{attributeID}",
        "type": "array",
        "items": {
          "type": "string"
        },
        "collectionFormat": "multi"
      },
      {
        "name": "fuzzymatching",
        "in": "query",
        "description": "fuzzy semantic matching of person names",
        "type": "boolean",
        "default": false
      },
      {
        "name": "offset",
        "in": "query",
        "description": "offset={skippedResults}",
        "type": "integer",
        "default": 0
      },
      {
        "name": "limit",
        "in": "query",
        "description": "limit={maximumResults}",
        "type": "integer"
      }
    ],
    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "type": "file"
        }
      },
      "204": {
        "description": "no matching instances found"
      },
      "404": {
        "description": "External AE Title not found"
      },
      "default": {
        "description": "unexpected error"
      }
    }
  }
},

此处提供完整的swagger文件

https://github.com/dcm4che/dcm4chee-arc-light/blob/master/dcm4chee-arc-ui2/src/swagger/swagger.json

或此处

http://petstore.swagger.io/index.html?url=https://raw.githubusercontent.com/dcm4che/dcm4chee-arc-light/master/dcm4chee-arc-ui2/src/swagger/swagger-dicom.json

任何主意是什么问题?

1 个答案:

答案 0 :(得分:2)

我修复了swagger文件中的一些错误:

  1. 未在参数集合中列出路径参数(这是上述问题的根本原因)
  2. 名称无效的参数(末尾包含!或包含在{}中)(这在生成时没有引起错误,但引起了编译错误)