RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

Could not find the JSON path of a referenced schema: Manually referenced schemas must be added to the 'Definitions' of a parent schema #1673

Open christophdebaene opened 6 months ago

christophdebaene commented 6 months ago

I get an error when calling ToJson. I don't understand why it works fine when the type is simply Customer, but I encounter an error when it's a list.

public record Customer(string FirstName, string LastName) 
{ 
}
var settings = new SystemTextJsonSchemaGeneratorSettings();
var document = new OpenApiDocument();

JsonSchemaGenerator generator = new(settings);
OpenApiSchemaResolver resolver = new(document, settings);

var customerListType = typeof(List<Customer>);

var schema = generator.Generate(customerListType, resolver);
var jschema = schema.ToJson();
MaximKitsenko commented 1 day ago

@christophdebaene Hi, did you find the solution?

MaximKitsenko commented 1 day ago

For me the problem was in:

        [Authorize(Policy = IdentityPolicies.AccountsCreateUsersPolicy)]
        [ProducesResponseType(typeof(List<CompositeItemOutDto>), StatusCodes.Status200OK)]
        [HttpGet("GetCategoriesAllTranslations")]
                public IActionResult GetCategoriesAllTranslations()
        {
                ...
            var result = someSvc.GetOutDtosAsync(TenantId);
            return Ok(result);
        }

So in ProducesResponseType - there is List but in reality it returns Task. After changing to the folowing code it started to work:

        public async Task<IActionResult> GetCategoriesAllTranslations()
        {
                ...
            var result = await someSvc.GetOutDtosAsync(TenantId);
            return Ok(result);
        }