2017-12-04 21 views
1

私は、ビルドしているサービスのAPIを定義するためにOpenAPI 3.0を使用しています。私は、他のコンポーネントの中でスキーマコンポーネントを再利用することに問題があります。たとえば、ノートを作成した人のProfileオブジェクトを含むNoteオブジェクトがあります。これは、$refキーワードを使用してProfileオブジェクトを参照することにより、正常に動作します。この問題は、プロファイルのデータがないという例を示しています。下のような例でrefを配置すると、Profileコンポーネントの実際のOpenAPIブロックProfileのデータだけでなく、実際のOpenAPIブロックも含まれます。Open API継承したサンプルデータ

他のコンポーネントでコンポーネントを再利用したり、これらのコンポーネントでサンプルセットを再利用する方法があるのだろうか?

例:

FullNote: 
    allOf: 
    - $ref: '#/components/schemas/BaseNote' 
    - type: object 
     title: A single note response 
     required: 
     - id 
     - dateCreated 
     - profile 
     properties: 
     id: 
      type: integer 
      format: int32 
     dateCreated: 
      type: integer 
      format: int64 
     profile: 
      type: object 
      $ref: '#/components/schemas/Profile' 
     example: 
     id: 123456789 
     dateCreated: 1509048083045 
     profile: 
      $ref: '#/components/schemas/Profile' 
+0

同じ質問が、OpenAPIを/ SWAGGER約2.0:(https://stackoverflow.com/q/47525254/113116)[闊歩のレスポンスの例で$ REFを使用する方法?] – Helen

答えて

2

例としては、インナー$refをサポートしていません。全体の例では、指定するインライン必要があります。また

FullNote: 
     allOf: 
     - $ref: '#/components/schemas/BaseNote' 
     - type: object 
      ... 
      example: 
      id: 123456789 
      dateCreated: 1509048083045 
      influencer: 
       prop1: value1 # <---- 
       prop2: value2 

を、あなたは、プロパティレベルの例を使用することができます - このケースで闊歩UIのようなツールは、プロパティの例からスキーマの例を構築します。

FullNote: 
     allOf: 
     - $ref: '#/components/schemas/BaseNote' 
     - type: object 
      ... 
      properties: 
      id: 
       type: integer 
       format: int32 
       example: 123456789  # <---- 
      dateCreated: 
       type: integer 
       format: int64 
       example: 1509048083045 # <---- 
      profile: 
       # This property will use examples from the Profile schema 
       $ref: '#/components/schemas/Profile' 
    Profile: 
     type: object 
     properties: 
     prop1: 
      type: string 
      example: value1 # <---- 
関連する問題