Open RicoSuter opened 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 👍
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
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.
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 :|
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?
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?
- 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
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;
}
}