christianhelle / refitter

Refit Client API Generator for OpenAPI
https://refitter.github.io
MIT License
155 stars 36 forks source link

Asana API specs strange naming #364

Open johnnypea opened 2 months ago

johnnypea commented 2 months ago

I noticed strange naming when using Asana API https://raw.githubusercontent.com/Asana/openapi/master/defs/asana_oas.yaml , like "Response78", "Anonymous60", or "Body33". Is this because of the bad specs? Can I do something about it?

[Headers("Accept: application/json")]
[Post("/projects")]
Task<Response78> CreateProject([Query(CollectionFormat.Multi)] IEnumerable<Anonymous60> opt_fields, [Query] bool? opt_pretty, [Body] Body33 body);
christianhelle commented 2 months ago

Yes, the schema can be improved, or more like simplified

This here:

      responses:
        201:
          description: Successfully created a new allocation.
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    $ref: '#/components/schemas/AllocationResponse'

generates this:

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class Response5
    {
        [JsonPropertyName("data")]
        public AllocationResponse Data { get; set; }

        private IDictionary<string, object> _additionalProperties;

        [JsonExtensionData]
        public IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties ?? (_additionalProperties = new Dictionary<string, object>()); }
            set { _additionalProperties = value; }
        }
    }

And that could be simplified to something like this:

      responses:
        201:
          description: Successfully created a new allocation.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AllocationResponse'

so there will be no need to generate an anonymous response wrapper that contains the data property with type AllocationResponse

but if the API itself returns a response like

{
  "data": { }
}

then you can't just change the OpenAPI spec for that API

christianhelle commented 2 months ago

The Asana API spec has 30k lines of code so I hope this wasn't written by hand 😄