Skip to content

Append Format property value to root schema when serializing as v2 if contained in anyOf, allOf, oneOf of child schemas #992

Closed

Description

When a primitive type schema of the example:

var schema = new OpenApiSchema();
if (context.Settings.IEEE754Compatible)
{
    schema.OneOf = new List<OpenApiSchema>
    {
        new OpenApiSchema { Type = "number", Format = "decimal" },
        new OpenApiSchema { Type = "string" },
    };
}

The expected serialization for v2 should look like below:

Age:
  format: int64

and the expected serialization for v3:

Age:
  oneOf:
    - type: integer
       format: int64
    - type: string
  nullable: true

This should be applicable to all primitive type schemas.

Activity

  1. changed the title [-]Remove `Format` property in the root schema when serializing primitive types as v3 if already contained in `anyOf`, `allOf``, `oneOf`[/-] [+]Remove `Format` property in the root schema when serializing primitive types as v3 if already contained in `anyOf`, `allOf`, `oneOf`[/+] on Aug 31, 2022
  2. baywet commented on Aug 31, 2022

    @baywet
    Member

    Thanks for creating the issue here.
    Before we proceed with a fix, I'd like to make sure we're on the same page on what to implement first.
    I do think the backward compatible aspects with V2 should be the sole responsibility of this library and it should not push decisions on dependents for that aspect.
    In different terms, I don't think that the OpenAPI.net.OData lib should have to set the format on the root schema "just in case we end up serializing to v2". But rather set things as they are expected to be in v3, and have this library handle the compatibility aspects.
    When this library needs to serialize as v2, it should see a schema is a composition (one/any of) and take the first type and format it can find to use from them.
    Does that make sense?

  3. MaggieKimani1 commented on Sep 1, 2022

    @MaggieKimani1
    Contributor

    When reproducing the above issue using the following schema:

    Age :
      oneOf :
        - type: number
          format: decimal
        - type: string
    

    I've noticed that during V2 serialization, any OneOf/AnyOf schema objects are written out as an allOf with only the first schema in the list as an attempt to downgrade gracefully and the rest are completely ignored.
    So the above schema would result to:

    Age :
      allOf :
        - format : decimal
          type : number
    
    

    with the type : string schema stripped out. Is this the intended behaviour Darrel (@darrelmiller), Vincent Biret (@baywet) and if so, why is that so?

  4. baywet commented on Sep 1, 2022

    @baywet
    Member

    I believe the first example in Irvine's initial post doesn't match the following ones and should instead have been

    if (context.Settings.IEEE754Compatible)
                        {
                            schema.OneOf = new List<OpenApiSchema>
                            {
                                new OpenApiSchema { Type = "integer", Format = "int64" },
                                new OpenApiSchema { Type = "string" }
                            };
                        }

    And sometimes we can even have something like

    if (context.Settings.IEEE754Compatible)
                        {
                            schema.OneOf = new List<OpenApiSchema>
                            {
                                new OpenApiSchema { Type = "integer", Format = format },
                                new OpenApiSchema { Type = "string" },
                                new OpenApiSchema
                                {
                                    Enum = new List<IOpenApiAny>
                                    {
                                        new OpenApiString("-INF"),
                                        new OpenApiString("INF"),
                                        new OpenApiString("NaN")
                                    }
                                
                                },
                        }

    In both cases I do think that taking the first entry and ignoring the remaining when serializing to v2 should get us to the right place.

  5. MaggieKimani1 commented on Sep 1, 2022

    @MaggieKimani1
    Contributor

    I believe the first example in Irvine's initial post doesn't match the following ones and should instead have been

    if (context.Settings.IEEE754Compatible)
                        {
                            schema.OneOf = new List<OpenApiSchema>
                            {
                                new OpenApiSchema { Type = "integer", Format = "int64" },
                                new OpenApiSchema { Type = "string" }
                            };
                        }

    And sometimes we can even have something like

    if (context.Settings.IEEE754Compatible)
                        {
                            schema.OneOf = new List<OpenApiSchema>
                            {
                                new OpenApiSchema { Type = "integer", Format = format },
                                new OpenApiSchema { Type = "string" },
                                new OpenApiSchema
                                {
                                    Enum = new List<IOpenApiAny>
                                    {
                                        new OpenApiString("-INF"),
                                        new OpenApiString("INF"),
                                        new OpenApiString("NaN")
                                    }
                                
                                },
                        }

    In both cases I do think that taking the first entry and ignoring the remaining when serializing to v2 should get us to the right place.

    Okay. I think I'm just curious why the rest of the schema components get ignored by design and what that achieves.

  6. baywet commented on Sep 1, 2022

    @baywet
    Member

    Ideally it wouldn't but since v2 doesn't support all/any of we need to make choices here. And the more important information is the fact that the client will get a int64 in the vast majority of cases, not a string, not an enum.

  7. irvinesunday commented on Sep 1, 2022

    @irvinesunday
    ContributorAuthor

    v2 supports allOf.
    Should oneOf and anyOf be collapsed into allOf when serializing as v2 as is currently the case?

  8. baywet commented on Sep 1, 2022

    @baywet
    Member

    aaaah I didn't know we had that option. The issue I can see with that approach is that the allOf will have conflicting entries, and we'll end up generating an allOf schema that can never be satisfied (e.g. "must be a string" and "must be an integer")

  9. changed the title [-]Remove `Format` property in the root schema when serializing primitive types as v3 if already contained in `anyOf`, `allOf`, `oneOf`[/-] [+]Append `Format` property to root schema when serializing as v2 if contained in `anyOf`, `allOf`, `oneOf` of child schemas[/+] on Sep 7, 2022
  10. changed the title [-]Append `Format` property to root schema when serializing as v2 if contained in `anyOf`, `allOf`, `oneOf` of child schemas[/-] [+]Append `Format` property value to root schema when serializing as v2 if contained in `anyOf`, `allOf`, `oneOf` of child schemas[/+] on Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

type:enhancementEnhancement request targeting an existing experience

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions