1

I am using NSwag for generating openapi 3 spec file. I am stuck at generating the Schema for the response types. Since I am using ServiceStack, I tried different options viz.,

Option 1

var generatorSettings = new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 };
        var schemaGenerator = new JsonSchemaGenerator(generatorSettings);
        var schemaResolver = new OpenApiSchemaResolver(document, generatorSettings);      

        var contextualReturnType = returnType.ToContextualType();
        var schema = schemaGenerator.GenerateWithReferenceAndNullability<JsonSchema>(contextualReturnType, false, schemaResolver);

        openApiOperationDescription.Operation.Responses[statusCode] = new OpenApiResponse
        {
            IsNullableRaw = false,
            Schema = schema
        };

Option 2

var generatorSettings = new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 };
        var schemaGenerator = new JsonSchemaGenerator(generatorSettings);
        var schemaResolver = new OpenApiSchemaResolver(document, generatorSettings);
        var schema = schemaGenerator.Generate(returnType);
  openApiOperationDescription.Operation.Responses[statusCode] = new OpenApiResponse
        {
            IsNullableRaw = false,
            Schema = schema
        };

Option 3

var schema = JsonSchema.FromType(returnType);
   openApiOperationDescription.Operation.Responses[statusCode] = new OpenApiResponse
        {
            IsNullableRaw = false,
            Schema = schema
        };

All these options have NOT generated the schema ref and the component section. Anything wrong I am doing? or any sample would be helpful.

Raajkumar
  • 857
  • 2
  • 13
  • 26

0 Answers0