programing

스프링 코드 생성 시 OpenAPI "one Of" 속성을 OpenAPI-generator-maven-plugin과 함께 사용하는 방법

megabox 2023. 2. 11. 09:10
반응형

스프링 코드 생성 시 OpenAPI "one Of" 속성을 OpenAPI-generator-maven-plugin과 함께 사용하는 방법

Angular 프론트 엔드 및 RESTful Spring Boot 백엔드를 사용하는 애플리케이션을 개발 중입니다.

org.openapitools에서 매우 편리한 maven 플러그인 openapi-generator-maven-plugin을 찾았습니다.코드 생성 기능을 통해 API의 프런트엔드와 백엔드 간에 "계약 우선" 방식을 적용할 수 있습니다.단, swagger 파일에서는 requestBody 및 responseBody 정의에 "One Of" 속성을 사용합니다.여기서 스프링 코드를 생성하려고 했지만 생성된 Java 클래스에서 Import가 누락되었습니다.

import com.pack.api.dto.OneOfLatteCoffeAmericanoCoffe;
import com.pack.api.dto.UNKNOWN_BASE_TYPE;

Swagger의 one Of 속성에 사용할 플러그인을 구성할 수 있는 방법이 있습니까?Spring Boot 2.3.1, Swagger 3.0 및 Openapi-generator-maven-plugin 4.3입니다.

현재 openapi-generator는 지원되지 않습니다.oneOf이것은 Open에서 새롭게 도입된 기능입니다.API v3 (참고: v2 이하만 "Swagger"로 불리며, 이후 OpenAPI로 이름이 변경되었습니다.)다양한 생성기(자바, 봄, 기타 언어)가 있습니다.나는 지원을 가능하게 하기 위해 올해 동안 기여가 이루어진 것을 보았다.

요약하자면 Open의 이 기능을 이용할 수 있을 때까지 조금 더 기다려야 할 것 같습니다.스프링용 제너레이터를 사용한 API v3 사양.

편집: "단기 로드맵"에도 기재되어 있습니다.

OAS 3.0 기능 지원: any Of, one Of, 콜백 등

스웨거를 변경할 수 있다면,oneOf추상적인 타입을 참조해 주세요.

예를 들어, 스웨거가 다음과 같은 경우:

components:
  schemas:
    'Parent':
      'vehicle':
        oneOf:
        - type: object
          properties:
            'car_property':
              type: string
        - type: object
          properties:
            'truck_property':
              type: string

다음과 같이 변경할 수 있습니다.

components:
  schemas:
    'Parent':
      type: object
      properties:
        'vehicle':
          $ref: '#/components/schemas/Vehicle'
    #---------------------------------------------------------------------------
    # Abstract class with discriminator 'vehicle_type'
    #---------------------------------------------------------------------------
    'Vehicle':
      type: object
      properties:
        'vehicle_type':
          type: string
          enum: [ 'CAR', 'TRUCK' ]
      discriminator:
        propertyName: vehicle_type
        mapping:
          'CAR': '#/components/schemas/Car'
          'TRUCK': '#/components/schemas/Truck'
    #---------------------------------------------------------------------------
    # Concrete classes
    #---------------------------------------------------------------------------
    'Car':
      allOf:
      - $ref: "#/components/schemas/Vehicle"
      - type: object
        properties:
          'car_property':
            type: string
    'Truck':
      allOf:
      - $ref: "#/components/schemas/Vehicle"
      - type: object
        properties:
          'truck_property':
            type: string

이 스웨거 수정은 제너레이터를 작동시킵니다.동일한 JSON 개체를 처리하지만 Open에서 의미론적으로 동등한지 100% 확신할 수 없습니다.API 사양

NAT은 다음과 같은 일부 발전기에 더 나은 지원 중 하나를 추가했습니다.java(okhttp-gson, jersey2, 네이티브),csharp-netcore, Go, PowerShell, R 등최신 마스터와 함께 시도해 보세요.스냅샷 버전은 프로젝트의 README에서 확인할 수 있습니다.https://github.com/OpenAPITools/openapi-generator/

@openapiitools/openapi-snots-cli를 사용하여 이 Java 스냅샷 https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.0.0-SNAPSHOT/openapi-generator-cli-$ {versionName}.jar를 사용했는데 작동했습니다.

셋업 openapitools.json이 필요합니다.

{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "6.0.0-20211025.061654-22",
    "repository": {
      "downloadUrl": "https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.0.0-SNAPSHOT/openapi-generator-cli-${versionName}.jar"
    }
  }
}

언급URL : https://stackoverflow.com/questions/62760156/how-to-use-openapi-oneof-property-with-openapi-generator-maven-plugin-when-gen

반응형