Forum Discussion

niqel504's avatar
niqel504
New Contributor
3 years ago

It is possible to select a schema only the fields that I need to return in a get

I have this api example

 

openapi: 3.0.1
info:
  title: ZodiacSign
  description: Api ZodiacSign (ASP.NET Core 3.1)
  termsOfService: https://www.zodiacSign.com/termsOfService
  contact:
    name: ZodiacSign
    url: www.zodiacSign.com
    email: 'support@zodiacSign.com.mx'
  version: 1.0.0
paths:
  /Sign:
    get:
      tags:
        - 'Sign'
      summary: 'Get All Zodiac Signs'
      responses:
        '200':
          description: OK
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/Sign'
            application/json:
              schema:
                $ref: '#/components/schemas/Sign'
            text/json:
              schema:
                $ref: '#/components/schemas/Sign'
            application/xml:
              schema:
                $ref: '#/components/schemas/Sign'
            text/xml:
              schema:
                $ref: '#/components/schemas/Sign'
        '400':
          description: Bad Request
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            application/json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            text/json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            application/xml:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            text/xml:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
        '401':
          description: Unauthorized
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            application/json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            text/json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            application/xml:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
            text/xml:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
components:
  schemas:
    ProblemDetails:
      type: object
      properties:
        type:
          type: string
          nullable: true
        title:
          type: string
          nullable: true
        status:
          type: integer
          format: int32
          nullable: true
        detail:
          type: string
          nullable: true
        instance:
          type: string
          nullable: true
    CreatedId:
      type: object
      properties:
        id:
          type: integer
          description: Gets or Sets Id
          format: int32
          nullable: true
    Sign:
      type: 'object'
      description: 'A zodiac sign represents a constellation in a time range'
      properties:
        id:
          type: 'integer'
          description: 'Represents its unique identifier number and its position among the 12 signs, it is not a self-incrementing number'
          example: 10
        name:
          type: 'string'
          description: 'Represents the descriptive name of the sign'
          example: 'Capricornus'
        intialDate:
          type: 'string'
          format: 'date-month'
          description: 'Represents the beginning of the time range'
          example: '22-12'
        finalDate:
          type: 'string'
          format: 'date-month'
          description: 'Represents the end of the time range'
          example: '20-01'
        elementId:
          type: 'integer'
          description: 'represents the identifying number of the element is unique and self-incrementing'
        element:
          $ref: '#/components/schemas/Element'
        planetId:
          type: 'integer'
          description: 'represents the identifying number of the planet is unique and self-incrementing'
        planet:
          $ref: '#/components/schemas/Planet'
      required:
        - id
        - name
        - intialDate
        - finalDate
        - elementId
        - planetId
    Element:
      type: 'object'
      properties:
        id:
          type: 'integer'
        name:
          type: 'string'
        signs:
          type: 'array'
          items:
            $ref: '#/components/schemas/Sign'
      required:
        - id
        - name
    Planet:
      type: 'object'
      properties:
        id:
          type: 'integer'
        name:
          type: 'string'
        signs:
          type: 'array'
          items:
            $ref: '#/components/schemas/Sign'
      required:
        - id
        - name
    
  securitySchemes:
    Bearer:
      type: http
      description: Por favor ingrese el token de la sesiĆ³n del usuario
      scheme: bearer
      bearerFormat: JWT
  responses:
    OK:
      description: OK
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    BadRequest:
      description: Bad Request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    Unauthorized:
      description: Unauthorized
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    NotFound:
      description: Not Found
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'

My question is 

If I only want to obtain the id and name of the signs entity, is it possible to make the api return only the fields that I require from this scheme ???

2 Replies

  • Hi niqel504 

     

    Thank you for providing the API definition, but I may not be understanding the question. Are you interested in describing an API / Operation that only includes those two fields? Or are you perhaps referring to _sparse fieldsets_? The latter has a nice example with the JSON:API framework here: https://jsonapi.org/examples/#sparse-fieldsets

    • niqel504's avatar
      niqel504
      New Contributor

      beatufiful the example Json:api is exactly I looking for

       

      but can you help me with example json:api with openApi, 

      I need tu combine both specifications. I need to document my api with open api and support the schenas json and endpoint like json:Api