openapi-processor / openapi-processor-base

re-usable code of openapi-processor
https://openapiprocessor.io
Apache License 2.0
2 stars 3 forks source link

Support for shared RequestBodies #2

Closed Snap252 closed 1 year ago

Snap252 commented 1 year ago

like given here https://swagger.io/docs/specification/describing-request-body/ -> "Reusable Bodies"

I would like to use this feature for code generation

example:

openapi: 3.0.0
x-stoplight:
  id: 79svnfnnbxapu
info:
  title: testApi
  version: '1.0'
servers:
  - url: 'http://localhost:3000'
paths:
  /user:
    post:
      summary: Create New User
      operationId: post-user
      responses:
        '200':
          description: User Created
          content:
            application/json:
              schema:
                type: string
        '400':
          description: Missing Required Information
        '409':
          description: Email Already Taken
      description: Create a new user.
      requestBody:
        $ref: '#/components/requestBodies/UserRequest'
components:
  schemas: {}
  requestBodies:
    UserRequest:
      content:
        application/json:
          schema:
            type: object
            properties:
              test:
                type: string

Exception via mvn:

[INFO] --- openapi-processor-maven-plugin:2021.1:process (generate-validation-api) @ kodi-nachrichten-service ---
[INFO] Changes detected - generating target files!
[ERROR] processing failed!
java.lang.NullPointerException: requestBody.content must not be null
    at io.openapiprocessor.core.parser.swagger.RequestBody.getContent (RequestBody.kt:38)
    at io.openapiprocessor.core.converter.ApiConverter.collectRequestBody (ApiConverter.kt:143)
    at io.openapiprocessor.core.converter.ApiConverter.createEndpoint (ApiConverter.kt:103)
    at io.openapiprocessor.core.converter.ApiConverter.createInterfaces (ApiConverter.kt:66)
    at io.openapiprocessor.core.converter.ApiConverter.convert (ApiConverter.kt:53)
    at io.openapiprocessor.spring.processor.SpringProcessor.run (SpringProcessor.kt:47)
    at io.openapiprocessor.maven.ProcessorRunner.run (ProcessorRunner.java:31)
    at io.openapiprocessor.maven.ProcessMojo.execute (ProcessMojo.java:60)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.120 s
[INFO] Finished at: 2022-12-20T09:53:33+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal io.openapiprocessor:openapi-processor-maven-plugin:2021.1:process (generate-validation-api) on project kodi-nachrichten-service: openapi-processor-spring execution failed!: requestBody.content must not be null -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

pom.xml:

...
  <properties>
    <openapi-processor-plugin.version>2021.1</openapi-processor-plugin.version>
    <openapi-processor-spring.version>2022.5</openapi-processor-spring.version>
  </properties>
...
      <plugin>
        <groupId>io.openapiprocessor</groupId>
        <artifactId>openapi-processor-maven-plugin</artifactId>
        <version>${openapi-processor-plugin.version}</version>
        <dependencies>
          <dependency>
            <groupId>io.openapiprocessor</groupId>
            <artifactId>openapi-processor-spring</artifactId>
            <version>${openapi-processor-spring.version}</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>generate-validation-api</id>
            <goals>
              <goal>process</goal>
            </goals>
            <!-- only to execute manually to update api. Generated code is versioned by git -->
            <phase>none</phase>
            <configuration>
              <id>spring</id>
              <apiPath>${openapi-spec.dir}/testApi.yaml</apiPath>
              <options>
                <values>
                  <targetDir>${generated-api.source-dir}</targetDir>
                  <mapping>${openapi-spec.dir}/mapping-validation.yaml</mapping>
                </values>
              </options>
            </configuration>
          </execution>
        </executions>
      </plugin>
hauner commented 1 year ago

interestingly, openapi processor doesn't explicitly check any of the components properties. I think it depends a bit on the openapi parser to help with it.

I have never used it. Usually I just $ref into another file which may be the reason I never noticed this before.

Will have to look at it in more detail.

Workaround is probably to $ref on the schema, ie.

schema:
  $ref: '....'

Thanks for reporting!

hauner commented 1 year ago

fixed in 2023.1