使用现有类与openapi-generator-maven-plugin,无需跳过验证

huangapple go评论55阅读模式
英文:

Use existing class with openapi-generator-maven-plugin without skipping validation

问题

我正在使用版本为6.3.0的插件openapi-generator-maven-plugin和openapi 3.0.3(yaml规范)。

我想使用现有的模型类而不是通过生成器创建它。我几乎在生成的API中成功使用了现有类,但我不得不通过设置<skipValidateSpec>true</skipValidateSpec>来跳过规范的验证。

有没有办法保持规范验证处于活动状态?

这是我的当前配置,它可以跳过验证而正常工作:

			<plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>6.3.0</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<inputSpec>
								${project.basedir}/src/main/resources/openapi.yaml
							</inputSpec>
							<generatorName>spring</generatorName>
							<apiPackage>com.example.openapigenerator.api</apiPackage>
							<skipValidateSpec>true</skipValidateSpec>
							<languageSpecificPrimitives>ArticleEntity</languageSpecificPrimitives>
							<importMappings>
								ArticleEntity=com.example.openapigenerator.model.Article
							</importMappings>
							<modelPackage>com.example.openapigenerator.model</modelPackage>
							<configOptions>
								<interfaceOnly>true</interfaceOnly>
								<useSpringBoot3>true</useSpringBoot3>
								<legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>
paths:
  /article:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Add a new article to the store
      operationId: addArticle
      requestBody:
        description: Create a new article in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ArticleEntity'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ArticleEntity'

components:
  schemas:
    ArticleEntity:
      $ref: '#/components/schemas/Article'
英文:

I am using plugin openapi-generator-maven-plugin in version 6.3.0 and openapi 3.0.3 (yaml spec).

I want to use an existing model class instead of creating it by generator. I almost used the existing class successfully in generated api, but I had to skip the validation of spec by setting <skipValidateSpec>true</skipValidateSpec>.

Is there a way of keeping the spec validation active?

Here is my current config which works with skipped validation:

			<plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>6.3.0</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<inputSpec>
								${project.basedir}/src/main/resources/openapi.yaml
							</inputSpec>
							<generatorName>spring</generatorName>
							<apiPackage>com.example.openapigenerator.api</apiPackage>
							<skipValidateSpec>true</skipValidateSpec>
							<languageSpecificPrimitives>ArticleEntity</languageSpecificPrimitives>
							<importMappings>
								ArticleEntity=com.example.openapigenerator.model.Article
							</importMappings>
							<modelPackage>com.example.openapigenerator.model</modelPackage>
							<configOptions>
								<interfaceOnly>true</interfaceOnly>
								<useSpringBoot3>true</useSpringBoot3>
								<legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>
paths:
  /article:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Add a new article to the store
      operationId: addArticle
      requestBody:
        description: Create a new article in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ArticleEntity'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ArticleEntity'

components:
  schemas:
    ArticleEntity:
      $ref: '#/components/schemas/Article'

答案1

得分: 2

使用外部模型很简单。假设我们有一个名为 MyExternalClass.java 的类。我们首先需要确保生成器知道这个类的位置。我们可以通过使用 importMappings 来实现这一点。然而,我们还需要告诉生成器要替换的对象是什么。这意味着我们必须有一个要替换的对象。让我们为我们的模式创建一个名为 MyUnusedClass 的虚拟对象。我们的最终模式将如下所示:

paths:
  /class:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Get an Object
      operationId: getObject
      requestBody:
        description: get my object
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MyInternalClass'
        required: true
      responses:
        '200':
          description: Successful operation
components:
  schemas:
    MyInternalClass:
      type: object
      properties:
        date:
          type: string
        name:
          type: string
        id:
          type: string
          format: uuid

现在我们已经走了一半。您的 Swagger 文档知道我们创建的虚拟模式。但是,您的生成器不知道此模式的外部版本。如我之前所解释的,我们可以轻松通过使用 typeMappings 来设置。

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.3.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
                    ${project.basedir}/src/main/resources/openapi.yaml
                </inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>com.example.openapigenerator.api</apiPackage>
                <skipValidateSpec>true</skipValidateSpec>
                <languageSpecificPrimitives>ArticleEntity</languageSpecificPrimitives>
                <importMappings>
                    MyExternalClass=my.package.containing.MyExternalClass
                </importMappings>
                <typeMappings>
                    MyInternalClas=MyExternalClass
                </typeMappings>
                <modelPackage>com.example.openapigenerator.model</modelPackage>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <useSpringBoot3>true</useSpringBoot3>
                    <legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

请注意,在上述 Maven 配置中,我不仅告诉生成器导入了该类,还告诉生成器要用 MyExternalClass 替换每一个 MyInternalClass 的出现。

奖励提示

即使生成器知道要用 MyExternalClass 替换 MyInternalClass 的出现,它仍会生成 Java 类。这占用空间并可能对您团队的其他开发人员造成困惑。为了防止这种情况,请将其添加到您的 .openapi-generator-ignore 文件中。现在就好像这个模式从未存在过一样。

英文:

Using an external model is simple. Let's pretend we have a class called MyExternalClass.java. We first need to make sure the generator knows where this class lives. We do this with out importMappings. However, we also need to tell our generator what object to replace. This means we have to have an object to replace. Let's create a dummy object for our schema called MyUnusedClass. Our resulting schema would look like this:

paths:
  /class:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Get an Object
      operationId: getObject
      requestBody:
        description: get my object
        content:
          application/json:
            schema:
              $ref: &#39;#/components/schemas/MyInternalClass&#39;
        required: true
      responses:
        &#39;200&#39;:
          description: Successful operation
components:
  schemas:
    MyInternalClass:
      type: object
      properties:
        date:
          type: string
        name:
          type: string
        id:
          type: string
          format: uuid

Now we are half way there. Your swagger documentation knows about the dummy schema that we have created. However, your generator doesn't know about the external version of this schema. As I explained previously, We can easily set that up with our typeMappings.

&lt;plugin&gt;
    &lt;groupId&gt;org.openapitools&lt;/groupId&gt;
    &lt;artifactId&gt;openapi-generator-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;6.3.0&lt;/version&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;goals&gt;
                &lt;goal&gt;generate&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
                &lt;inputSpec&gt;
                    ${project.basedir}/src/main/resources/openapi.yaml
                &lt;/inputSpec&gt;
                &lt;generatorName&gt;spring&lt;/generatorName&gt;
                &lt;apiPackage&gt;com.example.openapigenerator.api&lt;/apiPackage&gt;
                &lt;skipValidateSpec&gt;true&lt;/skipValidateSpec&gt;
                &lt;languageSpecificPrimitives&gt;ArticleEntity&lt;/languageSpecificPrimitives&gt;
                &lt;importMappings&gt;
                    MyExternalClass=my.package.containing.MyExternalClass
                &lt;/importMappings&gt;
                &lt;typeMappings&gt;
                    MyInternalClas=MyExternalClass
                &lt;/typeMappings&gt;
                &lt;modelPackage&gt;com.example.openapigenerator.model&lt;/modelPackage&gt;
                &lt;configOptions&gt;
                    &lt;interfaceOnly&gt;true&lt;/interfaceOnly&gt;
                    &lt;useSpringBoot3&gt;true&lt;/useSpringBoot3&gt;
                    &lt;legacyDiscriminatorBehavior&gt;false&lt;/legacyDiscriminatorBehavior&gt;
                &lt;/configOptions&gt;
            &lt;/configuration&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
&lt;/plugin&gt;

Note that in the above maven, I have not only told the generator to import the class, but I have told the generator to replace every occurrance of MyInternalClass with MyExternalClass.

BONUS TIP

Even though the generator knows to replace the occurances of MyInternalClass with MyExternalClass, it will still generate java class. This takes up space and can be confusing to other developers on your team. To prevent this, add it to your .openapi-generator-ignore file. Now it will be like the schema never existed in the first place.

答案2

得分: 0

以下是您要翻译的内容:

Taking @tbatch's answer further, the typeMappings brought me to the following solution:

paths:
  /article:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Add a new article to the store
      operationId: addArticle
      requestBody:
        description: Create a new article in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Article'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Article'

components:
  schemas:
    Article:
      type: object
      format: article
<plugin>
	<groupId>org.openapitools</groupId>
	<artifactId>openapi-generator-maven-plugin</artifactId>
	<version>6.3.0</version>
	<executions>
		<execution>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>
					${project.basedir}/src/main/resources/openapi.yaml
				</inputSpec>
				<generatorName>spring</generatorName>
				<apiPackage>com.example.openapigenerator.api</apiPackage>
				<skipValidateSpec>false</skipValidateSpec> <!-- My intended goal, to keep spec validation active -->
				<importMappings>
					Article=com.example.openapigenerator.model.Article
				</importMappings>
				<typeMappings>object+article=Article</typeMappings> <!-- THIS DID THE TRICK FOR ME -->
				<modelPackage>com.example.openapigenerator.model</modelPackage>
				<configOptions>
					<interfaceOnly>true</interfaceOnly>
					<useSpringBoot3>true</useSpringBoot3>
					<legacyDiscriminatorBehavior>false</legacyDiscriminatorBehavior>
				</configOptions>
			</configuration>
		</execution>
	</executions>
</plugin>
英文:

Taking @tbatch 's answer further, the typeMappings brougth me to the following solution:

paths:
  /article:
    post:
      tags:
        - article
      summary: Add a new article to the store
      description: Add a new article to the store
      operationId: addArticle
      requestBody:
        description: Create a new article in the store
        content:
          application/json:
            schema:
              $ref: &#39;#/components/schemas/Article&#39;
        required: true
      responses:
        &#39;200&#39;:
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: &#39;#/components/schemas/Article&#39;

components:
  schemas:
    Article:
      type: object
      format: article
&lt;plugin&gt;
	&lt;groupId&gt;org.openapitools&lt;/groupId&gt;
	&lt;artifactId&gt;openapi-generator-maven-plugin&lt;/artifactId&gt;
	&lt;version&gt;6.3.0&lt;/version&gt;
	&lt;executions&gt;
		&lt;execution&gt;
			&lt;goals&gt;
				&lt;goal&gt;generate&lt;/goal&gt;
			&lt;/goals&gt;
			&lt;configuration&gt;
				&lt;inputSpec&gt;
					${project.basedir}/src/main/resources/openapi.yaml
				&lt;/inputSpec&gt;
				&lt;generatorName&gt;spring&lt;/generatorName&gt;
				&lt;apiPackage&gt;com.example.openapigenerator.api&lt;/apiPackage&gt;
				&lt;skipValidateSpec&gt;false&lt;/skipValidateSpec&gt; &lt;!-- My intended goal, to keep spec validation active --&gt;
				&lt;importMappings&gt;
					Article=com.example.openapigenerator.model.Article
				&lt;/importMappings&gt;
				&lt;typeMappings&gt;object+article=Article&lt;/typeMappings&gt; &lt;!-- THIS DID THE TRICK FOR ME --&gt;
				&lt;modelPackage&gt;com.example.openapigenerator.model&lt;/modelPackage&gt;
				&lt;configOptions&gt;
					&lt;interfaceOnly&gt;true&lt;/interfaceOnly&gt;
					&lt;useSpringBoot3&gt;true&lt;/useSpringBoot3&gt;
					&lt;legacyDiscriminatorBehavior&gt;false&lt;/legacyDiscriminatorBehavior&gt;
				&lt;/configOptions&gt;
			&lt;/configuration&gt;
		&lt;/execution&gt;
	&lt;/executions&gt;
&lt;/plugin&gt;

huangapple
  • 本文由 发表于 2023年3月20日 23:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定