Forum Discussion

gresli's avatar
gresli
New Member
5 years ago

Put an object in a REST request header using OAS3

On this page https://swagger.io/docs/specification/serialization/ chapter "Header parameters" there is an example of a header object:

X-MyHeader: role=admin,firstName=Alex

how can this be done in OAS3?

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'

 

When creating the client code with the openapi-generator-maven-plugin version 4.3.0 and running the test I get this error message:

 

    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)

 

The code tries to put a newline character in the header, which is not allowed.

How can objects be put in the HTTP-Header using OAS3 and the OpenApi Generator?

No RepliesBe the first to reply