首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OAS3将对象放入REST请求头

使用OAS3将对象放入REST请求头
EN

Stack Overflow用户
提问于 2020-04-24 22:58:04
回答 1查看 511关注 0票数 1

在本页https://swagger.io/docs/specification/serialization/的"Header parameters“一章中,有一个header对象的示例:

X-MyHeader: role=admin,firstName=Alex

在OAS3中如何做到这一点呢?

这是我尝试过的:

代码语言:javascript
复制
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://localhost:8080/api
paths:
  /pet:
    post:
      summary: Create a pet
      operationId: addPet  
      tags:
        - pet
      parameters: 
        - name: X-MyHeader
          in: header
          schema:
            $ref: '#/components/schemas/X-MyHeader'
      requestBody:
        $ref: '#/components/requestBodies/postPet'
      responses:
        '200':
          description: Pet added

components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
    X-MyHeader:
        type: object
        properties:
          role:
            type: string
          firstName:
            type: string
  requestBodies:
    postPet:
      description: Pet object that needs to be added to the store
      required: true
      content:
         application/json:
          schema:
            $ref: '#/components/schemas/Pet'

在使用openapi-generator-maven-plugin版本4.3.0创建客户端代码并运行测试时,我收到以下错误消息:

代码语言:javascript
复制
    java.lang.IllegalArgumentException: Unexpected char 0x0a at 17 in X-MyHeader value: class XMyHeader {
    role: admin
    firstName: Alex
}
    at okhttp3.Headers$Companion.checkValue(Headers.kt:434)
    at okhttp3.Headers$Companion.access$checkValue(Headers.kt:346)
    at okhttp3.Headers$Builder.set(Headers.kt:328)
    at okhttp3.Request$Builder.header(Request.kt:199)
    at org.openapitools.client.ApiClient.processHeaderParams(ApiClient.java:1137)
    at org.openapitools.client.ApiClient.buildRequest(ApiClient.java:1036)
    at org.openapitools.client.ApiClient.buildCall(ApiClient.java:1010)
    at org.openapitools.client.api.PetApi.addPetCall(PetApi.java:101)
    at org.openapitools.client.api.PetApi.addPetValidateBeforeCall(PetApi.java:113)
    at org.openapitools.client.api.PetApi.addPetWithHttpInfo(PetApi.java:148)
    at org.openapitools.client.api.PetApi.addPet(PetApi.java:131)
    at org.openapitools.client.api.PetApiTest.addPetTest(PetApiTest.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

代码尝试在标题中放入换行符,这是不允许的。

如何使用OAS3和OpenApi生成器将对象放入Header中?

EN

回答 1

Stack Overflow用户

发布于 2021-02-02 22:43:13

当我自己研究在openapi 3.0中访问HTTP Request specific header information within the application本身的时候,我想我可能会偶然发现一个解决你的问题的方法。签出https://github.com/go-swagger/go-swagger/issues/1918,其中给出了在响应中创建自定义头部参数的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61411292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档