swaggest / swac

🤖 OpenAPI/Swagger client generator for PHP, Go and JavaScript (ES5)
MIT License
9 stars 3 forks source link

Required path params are generated as pointers #50

Open dmlambea opened 5 months ago

dmlambea commented 5 months ago

Running the latest released Docker image with following args: --with-zero-values --validate-required --skip-default-additional-properties

And feeding it with the following test input:

{
 "openapi": "3.0.3",
 "info": {
  "title": "Test API",
  "description": "Wrong pointer types in required path",
  "version": "1.0"
 },
 "paths": {
  "/v1/sample/operation": {
   "delete": {
    "tags": [
     "sample tag"
    ],
    "summary": "Required path parameters cannot be pointers",
    "description": "Sample test.",
    "parameters": [
     {
      "name": "ref",
      "in": "path",
      "required": true,
      "schema": {
       "type": "string"
      }
     },
     {
      "name": "version",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "204": {
      "description": "No Content"
     }
    }
   }
  }
 }
}

The resulting request is generated with pointer types. Moreover, the encoding method does not take the params into account when generating the URL:

// DeleteV1SampleOperationRequest is operation request value.
type DeleteV1SampleOperationRequest struct {
        Ref     *string  // Ref is a required `ref` parameter in path.
        Version *int64   // Version is a required `version` parameter in path.
}

// encode creates *http.Request for request data.
func (request *DeleteV1SampleOperationRequest) encode(ctx context.Context, baseURL string) (*http.Request, error) {
        requestURI := baseURL + "/v1/sample/operation"

        req, err := http.NewRequest(http.MethodDelete, requestURI, nil)
        if err != nil {
                return nil, err
        }

        req = req.WithContext(ctx)

        return req, err
}

Removing the arg --with-zero-values will not work because it disables generating optional attributes as pointers.