microsoft / OpenAPI.NET.CSharpAnnotations

Generates OpenAPI Document from C# Annotations
MIT License
195 stars 35 forks source link

IT DOESN'T WORK! #237

Open PeterAnaniaHC opened 3 years ago

PeterAnaniaHC commented 3 years ago

At all. You do exactly what the documentation says. You read your own XML document (you can even copy the XML document in the documentation and input that) and none of the members are translated into endpoints in the open api doc. It doesn't WORK!

Graz21 commented 3 years ago

Hey there,

I've just tested it out and it works. Make sure you've annotated your endpoints correctly.

Also you can check if there were errors during schema generation by inspecting the GenerationDiagnostic class.

Example of endpoint annotation:

/// <summary>
/// Load book by Id or Name
/// </summary>
/// <group>Books</group>
/// <verb>GET</verb>
/// <url>http://localhost:99/publicapi/books/{identifier}</url>
/// <param name="identifier" cref="string" in="path">Book id or name</param>
/// <response code="200"><see cref="BookDTO"/>Sample object retrieved</response>
[HttpGet]
public IHttpActionResult Get(string identifier)
{
    /* your code here */
}

Example for schema generation:

public string GenerateSchema() {

    var input = new OpenApiGeneratorConfig(
        annotationXmlDocuments: new List<XDocument>()
        {
            XDocument.Load(@"bin\project.xml") // your project xml
        },
        assemblyPaths: new List<string>()
        {
            @"bin\project.dll" // your project dll
        },
        openApiDocumentVersion: "V1",
        filterSetVersion: FilterSetVersion.V1
    );

    GenerationDiagnostic result;

    var generator = new OpenApiGenerator();

    IDictionary<DocumentVariantInfo, OpenApiDocument> openApiDocuments = generator.GenerateDocuments(
        openApiGeneratorConfig: input,
        generationDiagnostic: out result
    );

    // Check result for errors in:
    // OperationGenerationDiagnostic.Errors

    return openApiDocuments.First().Value.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
}

One thing I've noticed is that if you have complex types, like BookDTO in my case, its properties must have a getter and setter.

PeterAnaniaHC commented 3 years ago

Thank you very much for your time, but I was able to get it to work eventually. The issue I was having was a library conflict. I'm using .Net Core 3.1 and I was using the Microsoft.Extensions.Configuration NuGet package, but the two together (Open Api and the Extension) caused the solution to crash every time. I had to remove one of them, and since I needed the Open API document in order to expose the Azure function through the API Manager, I choose to keep the Open Api Generator. It was a little extra work, but as I said in the end I was able to get it to work.

It was aggravating though, because there were no library conflict warnings or errors and I literally had to start with a new application, add this library first then add every other package one at a time to find the issue. Arrggh.

In the end I think the issue is a conflict with the particular set of technologies I was using, not so much this library itself.

Again, thank you for your time, Peter

PeterAnaniaHC commented 3 years ago

Thank you.

You’re very kind.

I was able to get it to work.

I made the comment here, in case anyone else had the same issue - https://github.com/microsoft/OpenAPI.NET.CSharpAnnotations/issues/237?_pjax=%23js-repo-pjax-container

Thanks again,

Peter

From: Gonçalo Grazina @.> Sent: Monday, April 12, 2021 6:43 AM To: microsoft/OpenAPI.NET.CSharpAnnotations @.> Cc: Anania, Peter @.>; Author @.> Subject: Re: [microsoft/OpenAPI.NET.CSharpAnnotations] IT DOESN'T WORK! (#237)

Hey there,

I've just tested it out and it works. Make sure you've annotated your endpoints correctly.

Also you can check if there were errors during schema generation by inspecting the GenerationDiagnostic class.

Example of endpoint annotation:

///

/// Load book by Id or Name

///

/// Books

/// GET

/// http://localhost:99/publicapi/books/{identifier}</https://urldefense.com/v3/__http:/localhost:99/publicapi/books/*7Bidentifier*7D*3C/__;JSUl!!PH0vZokp8wwQNw!hVK6iDxeftGSHWvA-Zbwx6luB6VFBvw4dDxZzic7y8Ao9IZV5NslcUCxNNI0pFnIIzNalg$url>

/// Book id or name

/// Sample object retrieved

[HttpGet]

public IHttpActionResult Get(string identifier)

{

/* your code here */

}

Example for schema generation:

public string GenerateSchema() {

var input = new OpenApiGeneratorConfig(

    annotationXmlDocuments: new List<XDocument>()

    {

        XDocument.Load(@"bin\project.xml") // your project xml

    },

    assemblyPaths: new List<string>()

    {

        @"bin\project.dll" // your project dll

    },

    openApiDocumentVersion: "V1",

    filterSetVersion: FilterSetVersion.V1

);

GenerationDiagnostic result;

var generator = new OpenApiGenerator();

IDictionary<DocumentVariantInfo, OpenApiDocument> openApiDocuments = generator.GenerateDocuments(

    openApiGeneratorConfig: input,

    generationDiagnostic: out result

);

// Check result for errors

return openApiDocuments.First().Value.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

}

One thing I've noticed is that if you have complex types, like BookDTO in my case, its properties must have a getter and setter.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/microsoft/OpenAPI.NET.CSharpAnnotations/issues/237*issuecomment-817703263__;Iw!!PH0vZokp8wwQNw!hVK6iDxeftGSHWvA-Zbwx6luB6VFBvw4dDxZzic7y8Ao9IZV5NslcUCxNNI0pFlcSjWhqQ$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AMBH2HYGM4IYFSF4WLRARFLTILFC3ANCNFSM4ZJOGEYA__;!!PH0vZokp8wwQNw!hVK6iDxeftGSHWvA-Zbwx6luB6VFBvw4dDxZzic7y8Ao9IZV5NslcUCxNNI0pFlZ-xrSWA$.


Confidentiality Notice: This E-Mail is intended only for the use of the individual or entity to whom it is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If you have received this communication in error, please do not distribute and delete the original message. Please notify the sender by E-Mail at the address shown. Thank you for your compliance.