OpenAPI.NET.OData icon indicating copy to clipboard operation
OpenAPI.NET.OData copied to clipboard

Add support for @odata.context and @odata.count

Open sherlock1982 opened this issue 2 years ago • 3 comments

We have paging support and can specify $count=true to get the count of all elements. Unfortunately there's no way to get @odata.count value from a response.

Without this OData paging features are kind of unusable. In order to add it I modified the document approximately like this:

        foreach (var path in document.Paths.Values)
        {
            foreach (var operation in path.Operations.Values)
            {
                if (operation.OperationId.Contains(".List", StringComparison.Ordinal))
                {
                    var props = operation.Responses["200"].Content["application/json"].Schema.Properties;
                    props.Add("@odata.context", new OpenApiSchema()
                    {
                        Type = "string"
                    });
                    props.Add("@odata.count", new OpenApiSchema()
                    {
                        Type = "integer",
                        Format = "int32"
                    });
                }
           }
       }

sherlock1982 avatar Mar 11 '22 08:03 sherlock1982

Thanks for reporting this gap @sherlock1982 . @CarolKigoonya @irvinesunday I added this to the backlog (p3).

To provide more context on this one, we have recently moved to using component schemas for this to avoid duplication across endpoints.

I believe we should introduce a base type to avoid duplication of properties.

+    microsoft.graph.baseCollectionResponse:
+      title: Collection response
+      type: object
+      properties:
+        '@odata.nextLink':
+          type: string
+        '@odata.context':
+          type: string
+        '@odata.count':
+          type: int64
    microsoft.graph.userCollectionResponse:
+      allOf:
+         - '#/components/schemas/microsoft.graph.baseCollectionResponse'
+         - schema:
+               type: object
+               properties:
+                 value:
+                   type: array
+                   items:
+                      $ref: '#/components/schemas/microsoft.graph.user'
      title: Collection of user
      type: object
-      properties:
-        value:
-          type: array
-          items:
-            $ref: '#/components/schemas/microsoft.graph.user'
-        '@odata.nextLink':
-          type: string

baywet avatar Jul 05 '22 15:07 baywet

@sherlock1982 Do you have a need for @odata.context? I understand how count and nextlink are useful but to date the only tooling that I have seen that uses @odata.context is native OData tooling and therefore does not seem useful in an OpenAPI description.

darrelmiller avatar Jul 15 '22 17:07 darrelmiller

It's ok without @odata.context. Thought to add it just because it exists :-) I'm not sure though how nextLink can be natively supported.

sherlock1982 avatar Jul 15 '22 17:07 sherlock1982