Stream: dotnet
Topic: FHIR.Net Swagger/OpenAPI Missing Properties
Lars Ewert (Oct 27 2021 at 06:30):
Hey guys,
im using Swagger for my endpoint tests and figured out, that some objects from the Fhir Models lost some properties.
image.png
inside the Fhir.Model in my Backend i can clearly see the family (string) and given (string array) property
is it intended to be hidden from the OpenApi Definition?
Brian Postlethwaite (Oct 27 2021 at 21:18):
I don't think that swagger would be able to interpret the fhir object models from the c# classes. As there are some subtle differences between the json and xml formats.
Or do you mean that you're using the fhir swagger definition from the spec ?
Donn Relacion (Oct 28 2021 at 06:20):
The json schema generated by swashbuckle/MS OpenApi library for the FHIR C# types are not valid FHIR json. You would need to write code to build the OpenApiSchema based on those types. The default OpenApiSchema generator builds it based on the public properties of the type, which in the C# library contains the backing Element properties.
Brian Postlethwaite (Oct 28 2021 at 09:13):
The core fhir spec also has a pre generated open api definition, but many tools can't handle its scale and self referencing objects.
Lars Ewert (Oct 28 2021 at 19:49):
Well in general Swagger can interpret the Fhir models. But heres exactly what i mean:
image.png
inside the Fhir.Model HumanName exists a "public string Family" aswell as the "public FhirString FamilyElement" but inside Swagger (the screenshot from 1st post) i can only see the "FamilyElement" of type FhirString with its Properties inside. As you can see a simple string is not covered here.
I hope its more exactly what the my problem is.
Donn Relacion (Oct 29 2021 at 03:06):
I'm able to generate the swagger below by adding a custom swashbuckle IDocumentFilter that modifies the generated schemas. If the property type inherits from Hl7.Fhir.Mode.PrimitiveType, I rewrite the property to use the matching json type instead of the Fhir model type (e.g. FhirString is updated to string, Integer to integer, etc.). I also remove any properties that do not contain a FhirElementAttribute, and I use the name in this attribute.
I don't think it's a fool-proof solution, but it was good enough for our use case. I think using the pregenerated openapi definition should be the way to go, but then you'd have to embed these, and it wont work for custom resources.
Brian Postlethwaite (Oct 29 2021 at 23:35):
I'm also curious what benefit you're getting for these?
You're not going to be generating code from them, the documentation isn't that great, and the objects are really too complicated to be doing much more than simple gets, and for that of you put a HTML formatter in the pipeline, you can do that direct.
Brian Postlethwaite (Oct 29 2021 at 23:36):
Fully understand for custom, as that's not fhir anyway. And no open tools/packages work with them.
Brian Postlethwaite (Oct 29 2021 at 23:38):
Looking at that schema you have there, makes the extensibility harder to see (can't see it at all here) and most uses of fhir use that quite a bit.
Just looking at patient, us core has many on there, as do most countries for their local properties.
Lars Ewert (Oct 30 2021 at 10:29):
@Brian Postlethwaite i am generating Code from that documentation. I am using NSwag to generate my Frontend services with the respective Models. So if the generated models are wrong. My Frontend doesnt have the information and i have to manualy update these Models.
Meanwhile i am using the npm package @types/fhir which have more complete models to use.
But for fhir-extension i have to extend these models in my Frontend for sure.
Brian Postlethwaite (Oct 30 2021 at 11:03):
Yup, the @types/fhir is the good stuff all right. That gives you all the models.
There aren't any good generators for the extensions at present for typescript. @Gino Canessa is that something you know of some work?
Brian Postlethwaite (Oct 30 2021 at 11:06):
Those typescript classes are generated from the same source that generates the fhir net api models.
If you're buding a server I've done a facade layer if you wanted a kick start.
Gino Canessa (Nov 01 2021 at 15:28):
If you want the 'serializable' definitions for C#, the CSharpBasic_R*.cs
files should work from https://github.com/microsoft/fhir-codegen/tree/main/generated . It looks like I haven't generated all the 'newer' versions (as in the CSBasic2_R4 folder), but the R4 ones are there in that style.
The actual project in that repo can also output experimental OpenAPI definitions, but I haven't had much bandwidth to work on them recently. They should work for documentation, but I do not like the OpenAPI -> Code/Classes story at all. My biggest issue is that since OpenAPI doesn't know about the SDKs, the generated code that can't be used with them and thus isn't very useful.
Last updated: Apr 12 2022 at 19:14 UTC