Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating reference schema is not working #1947

Open
Blqck666 opened this issue Nov 20, 2024 · 1 comment
Open

Creating reference schema is not working #1947

Blqck666 opened this issue Nov 20, 2024 · 1 comment

Comments

@Blqck666
Copy link

Blqck666 commented Nov 20, 2024

Describe the bug
A clear and concise description of what the bug is.
i'm trying to build OpenApiDoc where i declare a predefined type, and i want to create a reference to that type dynamically.
i have object account , that have a field of type address , i want in openapi doc to reference the predefined object

OpenApi File To Reproduce
Add the OpenApi file you're using or a link to it as well as the steps to reproduce the current behavior.
the file is so big but theres some screenshots.
Image
Image

Expected behavior
A clear and concise description of what you expected to happen.
the field should have reference of type address. like this :
Image

Screenshots/Code Snippets
If applicable, add screenshots of the stack trace or a code snippet to help explain your problem.
If applicable, add a link to your project

public static void AddAddressSchema(this OpenApiDocument openApiDoc)
    {
        var addressSchema = new OpenApiSchema
        {
            Type = ObjectType,
            Properties = new Dictionary<string, OpenApiSchema>
            {
                { "City", new OpenApiSchema { Type = StringType, Nullable = true } },
                { "Country", new OpenApiSchema { Type = StringType, Nullable = true } },
                { "CountryCode", new OpenApiSchema { Type = StringType, Nullable = true } }
            }
        };

        openApiDoc.Components.Schemas.Add(AddressType, addressSchema);
    }

and to assign any field that have type address i'm doing this :

OpenApiSchema AssignPredefinedSchemaReference(string id)
        {
            return new OpenApiSchema
            {
                Reference = new OpenApiReference()
                {
                    Type = ReferenceType.Schema,
                    Id = id
                }
            };
        }

Additional context
Add any other context about the problem here.

@darrelmiller
Copy link
Member

When creating a reference there are two ways to set it up using the object model:

  1. Set the Reference property on your addressSchema instance to identify the Type and Id of the reference, but also use the same addressSchema instance in your Account.BillingAddress property. However, note that you can't set the description and nullable property on a reference object in OpenApi 3.0. You will need to use an allOf to capture the ability for the billing address to be nullable and provide a schema for address.
  2. Set the 'UnresolvedReference` property to true in your OpenApiSchema that just contains the Reference property. See You may then need to call the ResolveReferences method on OpenApiDocument depending on what you want to do with your OpenApiDocument.

Yes it is confusing. We have significantly re-vamped the way this works in the next major release of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants