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

Support generating values of type URL from JSON Schema #514

Open
Kyle-Ye opened this issue Jan 25, 2024 · 5 comments
Open

Support generating values of type URL from JSON Schema #514

Kyle-Ye opened this issue Jan 25, 2024 · 5 comments
Labels
area/generator Affects: plugin, CLI, config file. kind/enhacement Improvements to existing feature.
Milestone

Comments

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Jan 25, 2024

Motivation

{
	"time": "2023-12-06T20:45:17.385Z",
	"url": "https://example.com"
}

For the above json data. The currently valid openapi.yml will use string to represent "time" and "url".

I'm hoping to somehow add support for URL and Date types directly on swift-openapi-generator.

Proposed solution

  1. Add a variant for OpenAPI 3.1 to allow "url" and "date" for type. (Not recommended)

  2. ...

Alternatives considered

None

Additional information

No response

@Kyle-Ye Kyle-Ye added kind/feature New feature. status/triage Collecting information required to triage the issue. labels Jan 25, 2024
@MahdiBM
Copy link
Contributor

MahdiBM commented Jan 25, 2024

If correctly specified in openapi.yml:

created_at:
  type: string
  format: date-time

The generated Swift code will use the proper Date type:

/// - Remark: Generated from `#/components/schemas/MyType/created_at`.
package var created_at: Foundation.Date

I'm guessing you might be missing format: date-time or be using something that the generator can't use Date for.

@czechboy0
Copy link
Contributor

czechboy0 commented Jan 25, 2024

What @MahdiBM said is right, Date is already supported.

URL is not, and that's because a "URL" format is not officially supported in OpenAPI 3.0, but is kind of be supported in OpenAPI 3.1 through JSON Schema 2020-12: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-7.3.5

OpenAPI 3.1: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#data-types

I'll repurpose this issue to track adding "URL" format support.

@czechboy0 czechboy0 changed the title Add URL and Date parsing support Support generating values of type URL from JSON Schema Jan 25, 2024
@czechboy0 czechboy0 added area/generator Affects: plugin, CLI, config file. kind/enhacement Improvements to existing feature. and removed kind/feature New feature. status/triage Collecting information required to triage the issue. labels Jan 25, 2024
@czechboy0 czechboy0 added this to the Post-1.0 milestone Jan 25, 2024
@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Jan 26, 2024

If correctly specified in openapi.yml:

created_at:
  type: string
  format: date-time

The generated Swift code will use the proper Date type:

/// - Remark: Generated from `#/components/schemas/MyType/created_at`.
package var created_at: Foundation.Date

I'm guessing you might be missing format: date-time or be using something that the generator can't use Date for.

How can I specify the date format here?

For example, I have a date value in the response

{ "last_posted_at": "2023-12-06T20:45:17.385Z" }

In the past, I use a custom JSONDecoder here.

extension DateFormatter {
    public static var discourse: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
        formatter.timeZone = .gmt
        return formatter
    }
}

extension JSONDecoder {
    public static var discourse: JSONDecoder {
        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .formatted(.discourse)
        return decoder
    }
}

I guess now I need to config it via Client -> configuration: Configuration -> dateTranscoder: any DateTranscoder

@simonjbeaumont
Copy link
Contributor

simonjbeaumont commented Jan 26, 2024

Yes. We provided a new date transcoder in the latest release of the runtime library that handles ISO8601 with fractional seconds, which, looking at your JSON snippet, looks to be what you might need here.

https://swiftpackageindex.com/apple/swift-openapi-runtime/1.3.2/documentation/openapiruntime/datetranscoder/iso8601withfractionalseconds

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Jan 26, 2024

Yes. We provided a new date transcoder in the latest release of the runtime library that handles ISO8601 with fractional seconds, which, looking at your JSON snippet, looks to be what you might need here.

swiftpackageindex.com/apple/swift-openapi-runtime/1.3.2/documentation/openapiruntime/datetranscoder/iso8601withfractionalseconds

Thanks. I'll give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/generator Affects: plugin, CLI, config file. kind/enhacement Improvements to existing feature.
Projects
None yet
Development

No branches or pull requests

4 participants