Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

Prefer Json over other content types #180

Closed fairking closed 11 months ago

fairking commented 11 months ago

After introducing other content types I suggest to fall back on json if it exists. For example in some cases we would have multiple contents:

"paths": {
    "/Data/Address/GetAddress": {
      "post": {
        "tags": [
          "AddressService"
        ],
        "operationId": "GetAddress",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/IdVm"
              }
            },
            "application/xml": {
              "schema": {
                "$ref": "#/components/schemas/IdVm"
              }
            },
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/IdVm"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AddressFormVm"
                }
              },
              "application/xml": {
                "schema": {
                  "$ref": "#/components/schemas/AddressFormVm"
                }
              }
            }
          }
        },

Of course I am not mentioning xml, but as you can see if such schema exists we would prefer json over anything else. Any thoughts guys?

fairking commented 11 months ago

Tested, everything works fine on my project.

Manweill commented 11 months ago

Form-data is relatively special. If it exists, the form-data format should be used first.

Manweill commented 11 months ago

@fairking

fairking commented 11 months ago

Form-data is relatively special. If it exists, the form-data format should be used first.

Sorry for the late reply @Manweill . In this case they should exclude json I assume. I have other spec (see the topic's example) where several content types are included, so we might prefer json over other types.

Let me please know what do you think, and if you think it is still the case I can make changes so we can do your way.

But in my opinion it would be better if we can simply grab the content type from the scheme and include it into the produced TS. What do you think?

fairking commented 11 months ago

I have made some changes, so to get the actual content type from the schema instead. What do you think?

I have tested openapi (v3) successfully but not swagger schema. I tried this one but it wasn't working. Anyone can test swagger (v2) one?

Manweill commented 11 months ago

Form-data is relatively special. If it exists, the form-data format should be used first.

Sorry for the late reply @Manweill . In this case they should exclude json I assume. I have other spec (see the topic's example) where several content types are included, so we might prefer json over other types.

Let me please know what do you think, and if you think it is still the case I can make changes so we can do your way.

But in my opinion it would be better if we can simply grab the content type from the scheme and include it into the produced TS. What do you think?

Usually, we use json by default, but when we use formdata, we should give priority to formdata, because it has special purposes, such as uploading files.

fairking commented 11 months ago

Ok, I have made changes. It make sense and it doesn't introduce any breaking behavior.