RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Correctly handle parameter.schema in ToJson() instead of generator #1504

Open RicoSuter opened 6 years ago

RicoSuter commented 6 years ago
themultiplexer commented 6 years ago

Only as a side note in case you did not already see it: The Swagger Editor shows all the issues with the gernerated OpenAPI 3 spec. Following properties should also be inside the schema object:

And parameters added through an OperationProcessor should also use that schema object (currently not affected by c350820):

public class HeaderOperationProcessor : IOperationProcessor
{
    public async Task<bool> ProcessAsync(OperationProcessorContext context)
    {
        context.OperationDescription.Operation.Parameters.Add(new SwaggerParameter
        {
            Name = "X-MyProduct-Token",
            Description = "API Token",
            Kind = SwaggerParameterKind.Header,
            Type = NJsonSchema.JsonObjectType.String,
            IsRequired = true
        });
                return true;
        }
}

TIA 👍

RicoSuter commented 6 years ago

Not sure if "required" can be used from JSON Schema, it's defined as an array and specifies which object properties are required: https://tools.ietf.org/html/draft-wright-json-schema-validation-00#section-5.15

RicoSuter commented 6 years ago

The idea of this issue is to avoid assigning "schema" as it is currently done but copy parameter properties to "schema" when serializing to OpenAPI 3, this way you can keep this processor and serialization to OAI3 and Swagger2 works... not sure if it is possible to do.

themultiplexer commented 6 years ago

Oh ok now I get it! Then I should have commented elsewhere I guess... And yes required does not have to be in schema, sorry :|

stefanluchian commented 4 years ago

Hi. I use NSwagStudio v13.4.1.0 and NSwag.MsBuild v13.4.1. They still generate the Query-Paramaters using the old Swagger2 style, instead of the new OpenApi3 style (#1503 was closed, that's why I write here), even if I use "outputType": "OpenApi3" in the aspNetCoreToOpenApi. Is there maybe an extra switch to use the new style?

busbina commented 4 years ago

I'm on 13.5.0, set my schemaType to OpenApi3, but still have query parameters listed in the JSON instead of the object. Am I missing another settings?

janv8000 commented 1 year ago
  • Check if CreateUntypedPathParameter also needs to use "schema" instead of "type" directly

Our NSwag OpenAPI3 JSON generation relies heavily on settings.AddMissingPathParameters and thus on CreateUntypedPathParameter and the validator at https://editor.swagger.io/ marks each of these as out-of-spec because of the type usage:

Structural error at paths./v1/...post.parameters.0 should NOT have additional properties additionalProperty: type

      operationId: <snip>
      parameters:
        - type: string
          name: organisationId
          in: path
          required: true
SvenVMSW commented 1 year ago

I managed to get the following workaround working to get the output OpenApi3 compliant for now, create an OperationProcessor and add it to the settings OperationProcessors (I put it as the very last one):


    public class Swagger2ToOpenApi3ParameterOperationProcessor : IOperationProcessor   
    {  
        public bool Process(OperationProcessorContext context)   
        {  
            var parameterTypesToConvert = new[] { JsonObjectType.String, JsonObjectType.Boolean, JsonObjectType.Integer, JsonObjectType.Number };  
            for (var i = 0; i < context.OperationDescription.Operation.Parameters.Count; i++)
            {
                var parameter = context.OperationDescription.Operation.Parameters[i];
                if (parameterTypesToConvert.Contains(parameter.Type))
                {
                    var newParameter = new OpenApiParameter
                    {
                        Name = parameter.Name,
                        Kind = parameter.Kind,
                        IsRequired = parameter.IsRequired,
                        Description = parameter.Description,
                        Schema = new JsonSchema()
                        {
                            Type = parameter.Type,
                            Format = parameter.Format,
                            IsNullableRaw = parameter.IsNullableRaw
                        }
                    };
                    context.OperationDescription.Operation.Parameters[i] = newParameter;
                }
            }
            return true;
        }
    }